From 6614fac8e65831f9a3492c37edeab75ca24d9804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Fuglede=20J=C3=B8rgensen?= Date: Tue, 17 Dec 2024 07:20:59 +0100 Subject: [PATCH] Add solution to 2024-12-17 --- 2024/day17/solutions.py | 66 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2024/day17/solutions.py diff --git a/2024/day17/solutions.py b/2024/day17/solutions.py new file mode 100644 index 0000000..dc2f321 --- /dev/null +++ b/2024/day17/solutions.py @@ -0,0 +1,66 @@ +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 + + combo = [0, 1, 2, 3, A, B, C][op] + + match ins: + case 0: + A >>= combo + case 1: + B ^= op + case 2: + B = combo % 8 + case 3 if A: + ptr = op - 2 + case 4: + B ^= C + case 5: + result.append(combo % 8) + case 6: + B = A >> combo + case 7: + C = A >> combo + ptr += 2 + + +# Part 1 +print(",".join(map(str, 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)