Skip to content

Commit

Permalink
Add solution to 2024-12-17
Browse files Browse the repository at this point in the history
  • Loading branch information
fuglede committed Dec 17, 2024
1 parent 2db1026 commit 64a369b
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions 2024/day17/solutions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from itertools import count

program = [2, 4, 1, 1, 7, 5, 1, 5, 4, 5, 0, 3, 5, 5, 3, 0]


def run(A):
ptr = 0
B = 0
C = 0
result = []
while True:
try:
ins = program[ptr]
op = program[ptr + 1]
except:
return result
if op <= 3:
combo = op
elif op == 4:
combo = A
elif op == 5:
combo = B
elif op == 6:
combo = C

match ins:
case 0:
A //= 2**combo
case 1:
B ^= op
case 2:
B = combo % 8
case 3:
if A != 0:
ptr = op - 2
case 4:
B ^= C
case 5:
result.append(combo % 8)
case 6:
B = A // (2**combo)
case 7:
C = A // (2**combo)
ptr += 2


# Part 1
print(run(30344604))


# Part 2
mid = int(next(A for i in count() if len(run(A := 10**i)) == len(program))) * 10
width = mid
spacing = width // 1000
for matching_digits in range(1, len(program) + 1):
for A in range(mid - width, mid + width, spacing):
res = run(A)
if (
len(res) == len(program)
and res[-matching_digits:] == program[-matching_digits:]
):
break
else:
assert False
mid = A
spacing //= 10
width //= 10
if spacing <= 10:
spacing = 1
if width <= 10000:
width = 100000

print(A)

0 comments on commit 64a369b

Please sign in to comment.