Content-Length: 473058 | pFad | http://github.com/Piratmac/adventofcode-python/commit/5c53045ecb2a9f9d5d331e0652a2993d14bc264c

A1 Added day 2020-15 · Piratmac/adventofcode-python@5c53045 · GitHub
Skip to content

Commit 5c53045

Browse files
committed
Added day 2020-15
1 parent e6c39db commit 5c53045

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

2020/15-Rambunctious Recitation.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """0,3,6""",
38+
"expected": ["436", "175594"],
39+
}
40+
41+
test += 1
42+
test_data[test] = {
43+
"input": """1,3,2""",
44+
"expected": ["1", "175594"],
45+
}
46+
47+
test += 1
48+
test_data[test] = {
49+
"input": """2,1,3""",
50+
"expected": ["10", "3544142"],
51+
}
52+
53+
test += 1
54+
test_data[test] = {
55+
"input": """1,2,3""",
56+
"expected": ["27", "261214"],
57+
}
58+
59+
test += 1
60+
test_data[test] = {
61+
"input": """2,3,1""",
62+
"expected": ["78", "6895259"],
63+
}
64+
65+
test += 1
66+
test_data[test] = {
67+
"input": """3,2,1""",
68+
"expected": ["438", "18"],
69+
}
70+
71+
test += 1
72+
test_data[test] = {"input": """3,1,2""", "expected": ["1836", "362"]}
73+
74+
test = "real"
75+
input_file = os.path.join(
76+
os.path.dirname(__file__),
77+
"Inputs",
78+
os.path.basename(__file__).replace(".py", ".txt"),
79+
)
80+
test_data[test] = {
81+
"input": open(input_file, "r+").read(),
82+
"expected": ["763", "1876406"],
83+
}
84+
85+
86+
# -------------------------------- Control program execution ------------------------- #
87+
88+
case_to_test = "real"
89+
part_to_test = 2
90+
91+
# -------------------------------- Initialize some variables ------------------------- #
92+
93+
puzzle_input = test_data[case_to_test]["input"]
94+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
95+
puzzle_actual_result = "Unknown"
96+
97+
98+
# -------------------------------- Actual code execution ----------------------------- #
99+
100+
if part_to_test == 1:
101+
limit = 2020
102+
else:
103+
limit = 30000000
104+
105+
values = ints(puzzle_input)
106+
last_seen = {val: i + 1 for i, val in enumerate(values[:-1])}
107+
last_nr = values[-1]
108+
for i in range(len(values), limit):
109+
# #print ('before', i, last_nr, last_seen)
110+
if last_nr in last_seen:
111+
new_nr = i - last_seen[last_nr]
112+
last_seen[last_nr] = i
113+
else:
114+
last_seen[last_nr], new_nr = i, 0
115+
116+
# #print ('after', i, last_nr, new_nr, last_seen)
117+
# print (i+1, new_nr)
118+
last_nr = new_nr
119+
120+
puzzle_actual_result = new_nr
121+
122+
123+
# -------------------------------- Outputs / results --------------------------------- #
124+
125+
print("Case :", case_to_test, "- Part", part_to_test)
126+
print("Expected result : " + str(puzzle_expected_result))
127+
print("Actual result : " + str(puzzle_actual_result))
128+
# Date created: 2020-12-15 06:30:45.515647
129+
# Part 1: 2020-12-15 06:40:45
130+
# Part 2: 2020-12-15 07:33:58

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/Piratmac/adventofcode-python/commit/5c53045ecb2a9f9d5d331e0652a2993d14bc264c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy