diff options
author | Marvin Borner | 2024-12-24 11:36:38 +0100 |
---|---|---|
committer | Marvin Borner | 2024-12-24 11:36:38 +0100 |
commit | 7f3ae6c86e09d56acd51b82ad2c9f5c6795c104e (patch) | |
tree | feefe9247f2bf1e2a6a4da80dd291f63477e5815 /2024/24 | |
parent | 577a714d557270bdc1d87bdbe8f94b06aca0b8a8 (diff) |
oh du fröhliche
Diffstat (limited to '2024/24')
-rwxr-xr-x | 2024/24/solve.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/2024/24/solve.py b/2024/24/solve.py new file mode 100755 index 0000000..1a89949 --- /dev/null +++ b/2024/24/solve.py @@ -0,0 +1,38 @@ +#!/bin/env python3 + +L = open("input").read().strip().split("\n\n") + +wires = dict( + (l.split(": ")[0], int(l.split(": ")[1])) for l in L[0].split("\n") +) +conns = dict( + (l.split(" -> ")[1], l.split(" -> ")[0].split(" ")) + for l in L[1].split("\n") +) + +regs = set(wires.keys()) | set(conns.keys()) +zregs = sorted(r for r in regs if r[0] == "z")[::-1] + + +def eval(a, op, b): + match op: + case "AND": + return a & b + case "OR": + return a | b + case "XOR": + return a ^ b + case _: + print("AAAH") + return 0 + + +def solve1(reg): + if reg in wires: + return wires[reg] + a, op, b = conns[reg] + return eval(solve1(a), op, solve1(b)) + + +part1 = "".join(str(solve1(reg)) for reg in zregs) +print(int(part1, 2)) |