-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_07.py
executable file
·61 lines (52 loc) · 1.77 KB
/
day_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
#!/usr/bin/env python
class wire():
def __init__(self, s):
l = s.split()
self.name = l[-1]
self.known = False
self.value = 0
if any([op in l for op in ["AND", "OR", "LSHIFT", "RSHIFT"]]):
self.op = l[1]
self.source = [l[0], l[2]]
elif "NOT" in l:
self.op = "NOT"
self.source = l[1]
else:
self.op = "ID"
self.source = l[0]
def __str__(self):
return "Name: {}, op: {}, src: {}".format(self.name, self.op, self.source)
def sval(self, src):
if src.isdigit():
return int(src)
else:
return wiredict[src].getvalue()
def getname(self):
return self.name
def getvalue(self):
if not self.known:
self.known = True
if self.op == "ID":
self.value = self.sval(self.source)
elif self.op == "RSHIFT":
self.value = self.sval(self.source[0]) >> self.sval(self.source[1])
elif self.op == "LSHIFT":
self.value = self.sval(self.source[0]) << self.sval(self.source[1])
elif self.op == "NOT":
self.value = ~self.sval(self.source)
elif self.op == "OR":
self.value = self.sval(self.source[0]) | self.sval(self.source[1])
elif self.op == "AND":
self.value = self.sval(self.source[0]) & self.sval(self.source[1])
return self.value
wiredict = {}
with open("../input/input_07.txt", 'r') as f:
for line in f:
w = wire(line)
wiredict[w.getname()] = w
vala = wiredict['a'].getvalue()
print(vala)
for wire in wiredict.values():
wire.known = False
wiredict['b'].source = str(vala)
print(wiredict['a'].getvalue())