From 72574e753fa4a07645247bf7f25b17e134f89acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Fri, 3 Jan 2025 10:31:49 +0100 Subject: [PATCH] Solve 2024:24 pt1 "Crossed Wires" No idea how to solve pt2, so it will rest for now. --- 2024-python/output/day_24.py | 42 ++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 2024-python/output/day_24.py diff --git a/2024-python/output/day_24.py b/2024-python/output/day_24.py new file mode 100644 index 0000000..3474478 --- /dev/null +++ b/2024-python/output/day_24.py @@ -0,0 +1,42 @@ +def solve(data): + values, wires = data.split("\n\n") + values = dict([tuple(c.strip().split(": ")) for c in values.splitlines()]) + Q = [w.split() for w in wires.splitlines()] + while Q: + a, o, b, _, c = Q.pop(0) + if a not in values or b not in values: + Q.append((a, o, b, "_", c)) + continue + a, b = int(values[a]), int(values[b]) + match o: + case "AND": + values[c] = int(a + b == 2) + case "XOR": + values[c] = int(a + b == 1) + case "OR": + values[c] = int(a + b > 0) + p1 = int( + "".join( + [ + str(v) + for k, v in sorted(values.items(), key=lambda x: x[0], reverse=True) + if k.startswith("z") + ] + ), + 2, + ) + p2 = None + return p1, p2 + + +if __name__ == "__main__": + with open("./input/24.txt", "r") as f: + inp = f.read().strip() + + p1, p2 = solve(inp) + + print(p1) + print(p2) + + assert p1 == 36035961805936 + # assert p2 == 0