forked from tomkooij/AdventOfCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.1.py
82 lines (73 loc) · 2.43 KB
/
day7.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
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
#adventofcode.com
#day7.1
#
# Algorithm: Repeatedly process the list of gates. Removing processed gates
# logic was added "on the fly" -->
# "number AND gate -> gate" is implemented, but "number OR gate -> gate" is not.
INPUTFILE = 'input/input7'
#INPUTFILE = 'input/test7'
def AND(a, b): return a & b
def OR(a, b): return a | b
def RSHIFT(a, b): return a >> b
def LSHIFT(a, b): return a << b
def NOT(a, b): return ~a & 0xffff # unsigned int!
def do(f, in_a, in_b, out):
try:
a = signal[in_a]
except KeyError:
a = int(in_a)
try:
b = signal[in_b]
except KeyError:
b = int(in_b)
signal[out] = f(a, b)
signal = {}
with open(INPUTFILE) as f:
lines = f.readlines()
last = len(lines)+1
while len(lines) > 0:
print "lines to go", len(lines)
if len(lines) == last:
print "fail!", lines
break
last = len(lines)
for idx,line in enumerate(lines):
s = line.split()
if s[1] == 'AND':
assert s[3] == '->'
if (s[0].isdigit()) and (s[2] in signal):
lines.pop(idx)
do(AND, s[0], s[2], s[4])
if (s[0] in signal) and (s[2] in signal):
lines.pop(idx)
do(AND, s[0], s[2], s[4])
elif s[1] == 'OR':
assert s[3] == '->'
if (s[0] in signal) and (s[2] in signal):
lines.pop(idx)
do(OR, s[0], s[2], s[4])
elif s[1] == 'RSHIFT':
assert s[3] == '->'
if (s[0] in signal):
lines.pop(idx)
do(RSHIFT, s[0], s[2], s[4])
elif s[1] == 'LSHIFT':
assert s[3] == '->'
if (s[0] in signal):
lines.pop(idx)
do(LSHIFT, s[0], s[2], s[4])
elif s[0] == 'NOT':
assert s[2] == '->'
if (s[1] in signal):
lines.pop(idx)
do(NOT, s[1], 0, s[3])
elif s[1] == '->':
if s[0].isdigit():
signal[s[2]] = int(s[0])
lines.pop(idx)
elif(s[0] in signal):
signal[s[2]] = signal[s[0]]
lines.pop(idx)
else:
print "skipping unknown input: ", s
print "result: a = ", signal['a']