-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.py
executable file
·135 lines (119 loc) · 3.96 KB
/
07.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import re
pat1 = re.compile(r'^(\d+|[a-z]+) (AND|RSHIFT|LSHIFT|OR) (\d+|[a-z]+) -> ([a-z]+)$')
pat2 = re.compile(r'^NOT ([a-z]+) -> ([a-z]+)$')
pat3 = re.compile(r'^(\d+) -> ([a-z]+)$')
pat4 = re.compile(r'^([a-z]+) -> ([a-z]+)$')
wires = {}
inputs = {}
ops = {}
with open('07.txt' ) as file:
for line in (l.strip() for l in file):
m = pat1.match(line)
if m is not None:
if m.group(1) not in wires:
wires[m.group(1)] = None
if m.group(4) not in wires:
wires[m.group(4)] = None
if m.group(4) not in inputs:
inputs[m.group(4)] = set()
if not m.group(1).isdigit():
inputs[m.group(4)].add(m.group(1))
if not m.group(3).isdigit():
inputs[m.group(4)].add(m.group(3))
if m.group(4) not in ops:
ops[m.group(4)] = []
ops[m.group(4)].append({'op': m.group(2), 'left': m.group(1), 'right': m.group(3)})
continue
m = pat2.match(line)
if m is not None:
if m.group(1) not in wires:
wires[m.group(1)] = None
if m.group(2) not in wires:
wires[m.group(2)] = None
if m.group(2) not in inputs:
inputs[m.group(2)] = set()
inputs[m.group(2)].add(m.group(1))
if m.group(2) not in ops:
ops[m.group(2)] = []
ops[m.group(2)].append({'op': 'NOT', 'left': m.group(1), 'right': None})
continue
m = pat3.match(line)
if m is not None:
wires[m.group(2)] = int(m.group(1))
continue
m = pat4.match(line)
if m is not None:
if m.group(1) not in wires:
wires[m.group(1)] = None
if m.group(2) not in wires:
wires[m.group(2)] = None
if m.group(2) not in inputs:
inputs[m.group(2)] = set()
inputs[m.group(2)].add(m.group(1))
if m.group(2) not in ops:
ops[m.group(2)] = []
ops[m.group(2)].append({'op': 'SEND', 'left': m.group(1), 'right': None})
continue
pass
# uncomment for part 2
# wires['b'] = 16076
def exec(w, op):
l = op['left']
r = op['right']
ldig = l.isdigit()
rdig = r.isdigit() if r is not None else False
li = None
if ldig:
li = int(l)
ri = None
if rdig:
ri = int(r)
if op['op'] == 'SEND':
wires[w] = li if ldig else wires[l]
elif op['op'] == 'NOT':
wires[w] = ~ wires[l]
elif op['op'] == 'AND':
wires[w] = (li if ldig else wires[l]) & (ri if rdig else wires[r])
elif op['op'] == 'OR':
wires[w] = (li if ldig else wires[l]) | (ri if rdig else wires[r])
elif op['op'] == 'LSHIFT':
wires[w] = wires[l] << ri
elif op['op'] == 'RSHIFT':
wires[w] = wires[l] >> ri
pass
while len(ops) > 0:
opdone = False
wiresat = False
for w in wires:
if w not in inputs.keys() and w not in ops:
wiresat = True
for i in inputs:
if w in inputs[i]:
#print(clock, 'remove', w, 'from', i)
inputs[i].remove(w)
for w in wires:
if w in ops:
done = set()
for i, op in enumerate(ops[w]):
left = op['left']
right = op['right']
if left not in ops and (right is None or right not in ops):
#print(clock, 'exec', w, op)
opdone = True
exec(w, op)
done.add(i)
leftover = []
for i, op in enumerate(ops[w]):
if i in done:
continue
leftover.append(op)
if len(leftover) > 0:
ops[w] = leftover
else:
del ops[w]
if not opdone:
break
#print(wires)
#print(inputs)
#print(ops)
print(wires['a'])