Skip to content

Commit 23f1159

Browse files
committed
Added day 2020-10
1 parent b99833b commit 23f1159

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed

2020/10-Adapter Array.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
3+
from collections import Counter, deque, defaultdict
4+
from functools import lru_cache
5+
6+
from compass import *
7+
8+
9+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
10+
def lmap(func, *iterables):
11+
return list(map(func, *iterables))
12+
13+
14+
def ints(s: str):
15+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
16+
17+
18+
def positive_ints(s: str):
19+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
20+
21+
22+
def floats(s: str):
23+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
24+
25+
26+
def positive_floats(s: str):
27+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
28+
29+
30+
def words(s: str):
31+
return re.findall(r"[a-zA-Z]+", s)
32+
33+
34+
test_data = {}
35+
36+
test = 1
37+
test_data[test] = {
38+
"input": """16
39+
10
40+
15
41+
5
42+
1
43+
11
44+
7
45+
19
46+
6
47+
12
48+
4""",
49+
"expected": ["there are 7 differences of 1 jolt and 5 differences of 3 jolts", "8"],
50+
}
51+
52+
test = 2
53+
test_data[test] = {
54+
"input": """28
55+
33
56+
18
57+
42
58+
31
59+
14
60+
46
61+
20
62+
48
63+
47
64+
24
65+
23
66+
49
67+
45
68+
19
69+
38
70+
39
71+
11
72+
1
73+
32
74+
25
75+
35
76+
8
77+
17
78+
7
79+
9
80+
4
81+
2
82+
34
83+
10
84+
3""",
85+
"expected": ["22 differences of 1 jolt and 10 differences of 3 jolts", "19208"],
86+
}
87+
88+
test = "real"
89+
input_file = os.path.join(
90+
os.path.dirname(__file__),
91+
"Inputs",
92+
os.path.basename(__file__).replace(".py", ".txt"),
93+
)
94+
test_data[test] = {
95+
"input": open(input_file, "r+").read(),
96+
"expected": ["Unknown", "Unknown"],
97+
}
98+
99+
100+
# -------------------------------- Control program execution ------------------------- #
101+
102+
case_to_test = "real"
103+
part_to_test = 2
104+
105+
# -------------------------------- Initialize some variables ------------------------- #
106+
107+
puzzle_input = test_data[case_to_test]["input"]
108+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
109+
puzzle_actual_result = "Unknown"
110+
111+
112+
# -------------------------------- Actual code execution ----------------------------- #
113+
114+
if part_to_test == 1:
115+
joltages = ints(puzzle_input)
116+
my_joltage = max(joltages) + 3
117+
outlet = 0
118+
119+
diff_3 = 0
120+
diff_1 = 0
121+
122+
current_joltage = outlet
123+
while current_joltage != max(joltages):
124+
next_adapter = min([x for x in joltages if x > current_joltage])
125+
if next_adapter - current_joltage == 1:
126+
diff_1 += 1
127+
if next_adapter - current_joltage == 3:
128+
diff_3 += 1
129+
130+
current_joltage = next_adapter
131+
132+
diff_3 += 1
133+
puzzle_actual_result = (diff_1, diff_3, diff_1 * diff_3)
134+
135+
136+
else:
137+
joltages = ints(puzzle_input)
138+
joltages.append(max(joltages) + 3)
139+
joltages.append(0)
140+
edges = defaultdict(list)
141+
142+
for joltage in joltages:
143+
edges[joltage] = [x for x in joltages if x < joltage and x >= joltage - 3]
144+
145+
print(edges)
146+
147+
@lru_cache(maxsize=len(joltages))
148+
def count_paths(position):
149+
if position == 0:
150+
return 1
151+
else:
152+
nb_paths = 0
153+
# print (position, [count_paths(joltage) for joltage in edges[position]], edges[position])
154+
return sum([count_paths(joltage) for joltage in edges[position]])
155+
156+
puzzle_actual_result = count_paths(max(joltages))
157+
158+
159+
# -------------------------------- Outputs / results --------------------------------- #
160+
161+
print("Case :", case_to_test, "- Part", part_to_test)
162+
print("Expected result : " + str(puzzle_expected_result))
163+
print("Actual result : " + str(puzzle_actual_result))
164+
# Date created: 2020-12-10 06:00:02.437611

0 commit comments

Comments
 (0)