-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.py
executable file
·72 lines (63 loc) · 1.91 KB
/
day7.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
#!/usr/bin/env python
import os
import re
f = open('input/{}.txt'.format(os.path.splitext(os.path.basename(__file__))[0]),'r')
content = f.read()
f.close()
PATTERN = "^(((?P<fa>([a-z]{1,2})|\d) (?P<a>AND) (?P<ga>[a-z]{1,2}))|((?P<fo>[a-z]{1,2}) (?P<o>OR) (?P<go>[a-z]{1,2}))|((?P<fl>[a-z]{1,2}) (?P<l>LSHIFT) (?P<gl>\d+))|((?P<fr>[a-z]{1,2}) (?P<r>RSHIFT) (?P<gr>\d+))|(?P<c>(\d+)|([a-z]+))|((?P<n>NOT) (?P<fn>[a-z]{1,2}))) -> (?P<t>[a-z]+)$"
r = re.compile(PATTERN)
def f1(kv={}):
ld = []
while True:
nd = False
rs = False
for c in content.split('\n'):
m = r.match(c)
if(m and not c in ld and m.group('t') not in kv.keys()):
if(m.group('c')):
if(m.group('c').isdigit() or m.group('c') in kv.keys()):
if(m.group('c').isdigit()):
v = int(m.group('c'))
else:
v = kv[m.group('c')]
kv[m.group('t')] = v
ld.append(c)
rs = True
elif(m.group('a')):
if((m.group('fa') == "1" or m.group('fa') in kv.keys()) and m.group('ga') in kv.keys()):
if(m.group('fa') == "1"):
v = 1
else:
v = kv[m.group('fa')]
kv[m.group('t')] = v & kv[m.group('ga')]
ld.append(c)
rs = True
elif(m.group('o')):
if(m.group('fo') in kv.keys() and m.group('go') in kv.keys()):
kv[m.group('t')] = kv[m.group('fo')] | kv[m.group('go')]
ld.append(c)
rs = True
elif(m.group('l')):
if(m.group('fl') in kv.keys()):
kv[m.group('t')] = kv[m.group('fl')] << int(m.group('gl'))
ld.append(c)
rs = True
elif(m.group('r')):
if(m.group('fr') in kv.keys()):
kv[m.group('t')] = kv[m.group('fr')] >> int(m.group('gr'))
ld.append(c)
rs = True
elif(m.group('n')):
if(m.group('fn') in kv.keys()):
kv[m.group('t')] = ~kv[m.group('fn')] + 2**16
ld.append(c)
rs = True
if(rs):
nd = True
break
if(not nd):
break
return kv['a']
a = f1()
print(a)
print(f1({'b':a}))