aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-12-24 17:48:48 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-12-24 17:48:48 +0100
commit3774cc95387d2d334de0dd192e611b548f0989aa (patch)
tree0d6c4ac368e9e9586b4898e5247ed31456ccdba9
parent93687ac8dedab54af1973bdbe4929b4556023c72 (diff)
Add 2024 day 24 part 1 solution
-rwxr-xr-x2024/24/puzzle-1.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/2024/24/puzzle-1.py b/2024/24/puzzle-1.py
new file mode 100755
index 0000000..d2d27ca
--- /dev/null
+++ b/2024/24/puzzle-1.py
@@ -0,0 +1,46 @@
+#!/usr/bin/python3
+
+import collections
+from typing import Deque
+
+
+def main() -> None:
+ with open("input", "r") as f:
+ head, tail = [s.split("\n") for s in f.read().split("\n\n")]
+ wires: dict[str, int] = {}
+ for line in head:
+ wire, val = line.split(": ")
+ wires[wire] = int(val)
+ todo: Deque[list[str]] = collections.deque()
+ for line in tail:
+ parts = line.split()
+ parts.pop(3) # Pop ‘->’
+ todo.append(parts)
+
+ while todo:
+ eqn = todo[0]
+ if not (eqn[0] in wires and eqn[2] in wires):
+ todo.rotate(-1)
+ continue
+ else:
+ todo.popleft()
+ lhs = wires[eqn[0]]
+ rhs = wires[eqn[2]]
+ match eqn[1]:
+ case 'AND':
+ x = lhs & rhs
+ case 'OR':
+ x = lhs | rhs
+ case 'XOR':
+ x = lhs ^ rhs
+ wires[eqn[3]] = x
+
+ n = 0
+ ends = ((k, v) for k, v in wires.items() if k[0] == 'z')
+ for k, v in sorted(ends, reverse=True):
+ n = n<<1 | v
+ print(n)
+
+
+if __name__ == "__main__":
+ main() \ No newline at end of file