From 64a369b8bb4c362bd4a740f0317377cfa5151643 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 | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 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..738584f --- /dev/null +++ b/2024/day17/solutions.py @@ -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)