From 14520a4625ec40ad6bfd7490105989bb38250bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Sat, 21 Dec 2024 01:20:13 +0100 Subject: [PATCH] Solve 2024:17 p1-2 "Chronospatial Computer" Slow burner. First part was a straightforward implementation of a runner, the code is extracted to `org_version()`. Part 2 was way more tricky. Like many others, trying to understand the program felt necessary. This intepretation is this: """Python version of puzzle input program.""" a = b, c = 0, 0 while a: b = (a % 8) ^ 1 c = a // (2**b) b = (b ^ 5) ^ c a = a // (2**3) print(b % 8) Some clarification here: - the value is octagonal. 1-8 is the key. - The value of reg a is _high_. It is not meant to be brute forced. The idea, which is not originally mine, is to go from right to left. This code can be used to try out some patterns: while True: python_version(input("Provide A:")) Here, it was apparent a=4 gives the last digit of my puzzle input. a=37 (4 * 8 + 3) gives the last 2 digits. a=222 (37 * 8 + 6) gives the last 3 digits, and so on. Knowing the program could be reconstructed like this, the first code halted at wrong values. Turns out some steps give more than 1 possible scenario. The code was therefore in need of a queue. --- 2024-python/output/day_17.py | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 2024-python/output/day_17.py diff --git a/2024-python/output/day_17.py b/2024-python/output/day_17.py new file mode 100644 index 0000000..bbd5c0d --- /dev/null +++ b/2024-python/output/day_17.py @@ -0,0 +1,75 @@ +from collections import deque + +from output import ints + + +def solve(data): + R, program = data.split("\n\n") + R = {k: int(v) for k, v in zip("abc", ints(R))} + program = ints(program) + P = ",".join(map(str, program)) + + p1 = org_version(R["a"], program) + p2 = float("inf") + + Q = deque([1]) + while Q: + x = Q.popleft() + for i in range(8): + o = org_version(x + i, program) + if P.endswith(o): + if o == P: + p2 = min(p2, x + i) + else: + Q.append((x + i) * 8) + + return p1, p2 + + +def org_version(A, program): + R = {"a": A, "b": 0, "c": 0} + + def combo(v): + return v if v < 4 else R[chr(93 + v)] + + i = 0 + out = [] + while i < len(program): + cl = program[i + 1] + match program[i]: + case 0: + R["a"] = R["a"] // (2 ** combo(cl)) + i += 2 + case 1: + a, b = R["b"], cl + R["b"] = a ^ b + i += 2 + case 2: + R["b"] = combo(cl) % 8 + i += 2 + case 3: + i = (i + 2) if R["a"] == 0 else cl + case 4: + a, b = R["b"], R["c"] + R["b"] = a ^ b + i += 2 + case 5: + out.append(combo(cl) % 8) + i += 2 + case 6: + R["b"] = R["a"] // (2 ** combo(cl)) + i += 2 + case 7: + R["c"] = R["a"] // (2 ** combo(cl)) + i += 2 + return ",".join(map(str, out)) + + +if __name__ == "__main__": + with open("./input/17.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2)