-
Notifications
You must be signed in to change notification settings - Fork 0
/
7-1.py
34 lines (29 loc) · 864 Bytes
/
7-1.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
import re
lines = open('7.txt').read().replace('\r', '').split('\n')
wires = {}
ops = {
r'^NOT (\w+) -> (\w+)$': lambda a: 65535-a,
r'^(\w+) OR (\w+) -> (\w+)$': lambda a, b: a|b,
r'^(\w+) AND (\w+) -> (\w+)$': lambda a, b: a&b,
r'^(\w+) LSHIFT (\w+) -> (\w+)$': lambda a, b: a<<b,
r'^(\w+) RSHIFT (\w+) -> (\w+)$': lambda a, b: a>>b,
r'^(\w+) -> (\w)$': lambda a: a
}
def get_value(w, a):
b = w.get(a)
if b == None:
b = int(a)
return b
while lines:
line = lines.pop(0)
try:
for op, f in ops.iteritems():
m = re.match(op, line)
if m:
args, target = m.groups()[:-1], m.groups()[-1]
args = [get_value(wires, a) for a in args]
wires[target] = f(*args)
break
except:
lines.append(line)
print wires['a']