-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7-2.py
More file actions
36 lines (31 loc) · 939 Bytes
/
7-2.py
File metadata and controls
36 lines (31 loc) · 939 Bytes
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
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)
if target == 'b':
wires[target] = 3176
break
except:
lines.append(line)
print wires['a']