Skip to content

Commit 6614fac

Browse files
committed
Add solution to 2024-12-17
1 parent 2db1026 commit 6614fac

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

2024/day17/solutions.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 // 1000
48+
for matching_digits in range(1, len(program) + 1):
49+
for A in range(mid - width, mid + width, spacing):
50+
res = run(A)
51+
if (
52+
len(res) == len(program)
53+
and res[-matching_digits:] == program[-matching_digits:]
54+
):
55+
break
56+
else:
57+
assert False
58+
mid = A
59+
spacing //= 10
60+
width //= 10
61+
if spacing <= 10:
62+
spacing = 1
63+
if width <= 10000:
64+
width = 100000
65+
66+
print(A)

0 commit comments

Comments
 (0)