aboutsummaryrefslogtreecommitdiff
path: root/2024/24/puzzle-1.py
blob: d2d27ca79e0c70aec0352c4b17679b916eaa363a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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()