This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.py
165 lines (130 loc) · 3.52 KB
/
7.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# import sys
# inputdata = sys.stdin.readlines()
# data = [x.rstrip('\n') for x in inputdata]
# import re
# prog_0 = re.compile(r'^(\d*|\w*) -> (\w*)$')
# prog_1 = re.compile(r'^(NOT) (\d*|\w*) -> (\w*)$')
# prog_2 = re.compile(r'^(\d*|\w*) (AND|OR|LSHIFT|RSHIFT) (\d*|\w*) -> (\w*)$')
# def resolve(f):
# r = resolve(f)
# if callable(r):
# return resolve(r)
# return r
# def handle_0(wires, x, o):
# xv = lambda: resolve(wires[x]) if x.isalpha() else int(x)
# wires[o] = xv
# def handle_1(wires, x, op, o):
# xv = lambda: resolve(wires[x]) if x.isalpha() else int(x)
# if op == 'NOT':
# wires[o] = lambda: ~xv()
# else:
# print "UNHANDLED!!!"
# def handle_2(wires, x, y, op, o):
# xv = lambda: wires[x] if x.isalpha() else int(x)
# yv = lambda: wires[y] if y.isalpha() else int(y)
# if op == 'AND':
# wires[o] = lambda: xv() & yv()
# elif op == 'OR':
# wires[o] = lambda: xv() | yv()
# elif op == 'LSHIFT':
# wires[o] = lambda: xv() << yv()
# elif op == 'RSHIFT':
# wires[o] = lambda: xv() >> yv()
# else:
# print "UNHANDLED!!!"
# from collections import defaultdict
# wires = defaultdict(int)
# for d in data:
# handled = False
# m = prog_0.match(d)
# if m:
# x = m.group(1)
# o = m.group(2)
# handle_0(wires, x, o)
# m = prog_1.match(d)
# if m:
# x = m.group(2)
# o = m.group(3)
# op = m.group(1)
# handle_1(wires, x, op, o)
# m = prog_2.match(d)
# if m:
# x = m.group(1)
# y = m.group(3)
# o = m.group(4)
# op = m.group(2)
# handle_2(wires, x, y, op, o)
# print wires['a']()
import sys
inputdata = sys.stdin.readlines()
data = [x.rstrip('\n') for x in inputdata]
import re
prog_0 = re.compile(r'^(\d*|\w*) -> (\w*)$')
prog_1 = re.compile(r'^(NOT) (\d*|\w*) -> (\w*)$')
prog_2 = re.compile(r'^(\d*|\w*) (AND|OR|LSHIFT|RSHIFT) (\d*|\w*) -> (\w*)$')
def handle_0(wires, x, o):
if not x.isalpha():
wires[o] = int(x)
else:
if x in wires:
wires[o] = wires[x]
def handle_1(wires, x, op, o):
if op == 'NOT':
f = lambda p1: ~p1
else:
print "UNHANDLED!!!"
return
if not x.isalpha():
wires[o] = f(int(x))
else:
if x in wires:
wires[o] = f(wires[x])
def handle_2(wires, x, y, op, o):
if op == 'AND':
f = lambda p1,p2: p1 & p2
elif op == 'OR':
f = lambda p1,p2: p1 | p2
elif op == 'LSHIFT':
f = lambda p1,p2: p1 << p2
elif op == 'RSHIFT':
f = lambda p1,p2: p1 >> p2
else:
print "UNHANDLED!!!"
return
if not x.isalpha():
xv = int(x)
else:
if x in wires:
xv = wires[x]
else:
return
if not y.isalpha():
yv = int(y)
else:
if y in wires:
yv = wires[y]
else:
return
wires[o] = f(xv, yv)
wires = {}
while not 'a' in wires:
for d in data:
m = prog_0.match(d)
if m:
x = m.group(1)
o = m.group(2)
handle_0(wires, x, o)
m = prog_1.match(d)
if m:
x = m.group(2)
o = m.group(3)
op = m.group(1)
handle_1(wires, x, op, o)
m = prog_2.match(d)
if m:
x = m.group(1)
y = m.group(3)
o = m.group(4)
op = m.group(2)
handle_2(wires, x, y, op, o)
print wires['a']