-
Notifications
You must be signed in to change notification settings - Fork 0
/
202424.py
90 lines (75 loc) · 2.77 KB
/
202424.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import re
from functools import cache
from typing import Dict, List, Set, Tuple
from utils import input_as_strings_iter
inputs: Dict[str, int] = dict()
net: Dict[str, Tuple[str, List[str]]] = dict()
all_wires: Set[str] = set()
part_one = True
for line in input_as_strings_iter("202424.txt"):
if line == "":
part_one = False
continue
elif part_one:
label, value = line.split(": ")
inputs[label] = int(value)
all_wires.add(label)
else:
[a, op, b, c] = re.match(r"(.{3}) (AND|OR|XOR) (.{3}) -> (.{3})", line).groups()
net[c] = (op, [a, b])
all_wires.add(a)
all_wires.add(b)
all_wires.add(c)
@cache
def resolve(wire: str) -> int:
if wire in inputs:
return inputs[wire]
op, [a, b] = net[wire]
aval = resolve(a)
bval = resolve(b)
if op == "AND":
return aval & bval
elif op == "OR":
return aval | bval
elif op == "XOR":
return aval ^ bval
else:
raise ValueError(f"Unknown operation: {op}")
z = {wire: resolve(wire) for wire in all_wires if wire.startswith("z")}
part_one = sum(z[wire] << i for i, wire in enumerate(sorted(z.keys())))
print(f"Part one: {part_one}")
def part_two():
faulty = set()
last_z = sorted(z.keys())[-1]
def is_input_wire(*wires: str) -> bool:
return all(wire.startswith("x") or wire.startswith("y") for wire in wires)
def is_output_wire(*wires: str) -> bool:
return all(wire.startswith("z") for wire in wires)
def is_first_bit(*wires: str) -> bool:
return all(wire.endswith("00") for wire in wires)
for output, (op, [a, b]) in net.items():
is_faulty = False
if is_output_wire(output) and output != last_z:
# All outputs except the last one should be fed by an XOR
is_faulty = op != "XOR"
elif not is_output_wire(output) and not is_input_wire(a, b):
# No intermediate gates should be an XOR
is_faulty = op == "XOR"
elif is_input_wire(a, b) and not is_first_bit(a, b):
# If the wire is being fed by two input wires, but not the first bits
# (because they start the chain and are special) then XOR should feed
# into XOR (for addition) and AND should feed into OR (for carry).
expected = "XOR" if op == "XOR" else "OR"
feeds = False
for c, (op2, [a2, b2]) in net.items():
if c == output:
continue
if (a2 == output or b2 == output) and op2 == expected:
feeds = True
if feeds:
break
is_faulty = not feeds
if is_faulty:
faulty.add(output)
return ",".join(sorted(faulty))
print(f"Part two: {part_two()}")