-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
102 lines (81 loc) · 2.26 KB
/
script.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
91
92
93
94
95
96
97
98
99
100
101
102
import sys
import operator
import itertools
def print_line(line):
l, op, r, output
match op:
case 'AND' | 'OR' | 'LSHIFT' | 'RSHIFT':
print(f'{l} {op} {r} -> {output}')
case 'NOT':
print(f'{op} {r} -> {output}')
case _:
print(f'{l} -> {output}')
def not_16b(val):
return ~val & 0xffff
ops = {'AND': operator.and_,
'OR': operator.or_,
'LSHIFT': operator.lshift,
'RSHIFT': operator.rshift,
'NOT': not_16b}
def wire_value(wv):
if wv is None:
return None
elif wv in wires:
return wires[wv]
elif wv.isnumeric():
return int(wv)
def process(line):
l, op, r, output = line
l = wire_value(l)
r = wire_value(r)
match op:
case 'AND' | 'OR' | 'LSHIFT' | 'RSHIFT':
if l is not None and r is not None:
wires[output] = ops[op](l, r)
return True
case 'NOT':
if r is not None:
wires[output] = ops[op](r)
return True
case _:
if l is not None:
wires[output] = l
return True
return False
def simulate(circuit, wires):
while True:
wires_done = len(wires)
for index, line in enumerate(circuit):
if line[3] not in wires:
process(line)
if len(wires) == wires_done:
break
return wires
if __name__ == '__main__':
inputs = (line.rstrip('\n') for line in open(sys.argv[1]))
circuit = []
for line in inputs:
instr, output = line.split(' -> ')
instr = instr.split(' ')
l, op, r = None, None, None
try:
if len(instr) == 1:
l = instr[0]
elif len(instr) == 2:
op, r = instr
elif len(instr) == 3:
l, op, r = instr
except ValueError as e:
print(line)
print(e)
circuit.append((l, op, r, output))
wires = {}
wires = simulate(circuit, wires)
#for k, v in sorted(wires.items()):
# print(f'{k}: {v}')
print(f"a: {wires['a']}")
a = wires['a']
wires = {}
wires['b'] = a
wires = simulate(circuit, wires)
print(f"a: {wires['a']}")