-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07_Some_Assembly_Required.py
More file actions
50 lines (39 loc) · 1.42 KB
/
Copy path07_Some_Assembly_Required.py
File metadata and controls
50 lines (39 loc) · 1.42 KB
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
from typing import Dict, Callable
from functools import cache
lines = [l.strip() for l in open('07_input.txt')]
MASK16 = 0xFFFF
def build_board() -> Dict[str, Callable[[], int]]:
wires = dict()
def build_op(source):
if 'NOT' in source:
input_wire = source.split()[1]
return lambda: ~wires[input_wire]()
elif 'LSHIFT' in source:
input_wire, shift = source.split(' LSHIFT ')
return lambda: (wires[input_wire]() << int(shift)) & MASK16
elif 'RSHIFT' in source:
input_wire, shift = source.split(' RSHIFT ')
return lambda: wires[input_wire]() >> int(shift)
elif '1 AND' in source:
wire = source.split()[-1]
return lambda: wires[wire]() & 1
elif 'AND' in source:
wire1, wire2 = source.split(' AND ')
return lambda: wires[wire1]() & wires[wire2]()
elif 'OR' in source:
wire1, wire2 = source.split(' OR ')
return lambda: wires[wire1]() | wires[wire2]()
elif source.isdecimal():
return lambda: int(source)
else:
return lambda: wires[source]()
for line in open('07_input.txt'):
source, wire = line.strip().split(' -> ')
wires[wire] = cache(build_op(source))
return wires
b = build_board()
tmp = b['a']()
print('Star 1:', tmp)
b = build_board()
b['b'] = lambda: tmp
print('Star 2:', b['a']())