Skip to content

Commit 6d864e6

Browse files
committed
Add solution to 2024-12-17
1 parent 2db1026 commit 6d864e6

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

2024/day17/solutions.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from itertools import count
2+
3+
program = [2, 4, 1, 1, 7, 5, 1, 5, 4, 5, 0, 3, 5, 5, 3, 0]
4+
5+
6+
def run(A):
7+
ptr = 0
8+
B = 0
9+
C = 0
10+
result = []
11+
while True:
12+
try:
13+
ins = program[ptr]
14+
op = program[ptr + 1]
15+
except:
16+
return result
17+
18+
combo = [0, 1, 2, 3, A, B, C][op]
19+
20+
match ins:
21+
case 0:
22+
A >>= combo
23+
case 1:
24+
B ^= op
25+
case 2:
26+
B = combo % 8
27+
case 3 if A:
28+
ptr = op - 2
29+
case 4:
30+
B ^= C
31+
case 5:
32+
result.append(combo % 8)
33+
case 6:
34+
B = A >> combo
35+
case 7:
36+
C = A >> combo
37+
ptr += 2
38+
39+
40+
# Part 1
41+
print(",".join(map(str, run(30344604))))
42+
43+
44+
# Part 2
45+
mid = int(next(A for i in count() if len(run(A := 10**i)) == len(program))) * 10
46+
width = mid
47+
spacing = width // 100
48+
for matching_digits in range(1, len(program) + 1):
49+
mid = next(
50+
A
51+
for A in range(mid - width, mid + width, spacing)
52+
if len(run(A)) == len(program)
53+
and run(A)[-matching_digits:] == program[-matching_digits:]
54+
)
55+
spacing //= 10
56+
width //= 10
57+
if spacing <= 10:
58+
spacing = 1
59+
if width <= 10000:
60+
width = 100000
61+
62+
print(mid)

0 commit comments

Comments
 (0)