-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2221.py
71 lines (64 loc) · 1.72 KB
/
aoc2221.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
import sympy
def main():
fd = 0#1#0
path = '_inputs/2221.'
data = open(path + str(fd)).read()
print('Star 1:', p1(data))
print('Star 2:', p2(data))
# part 2
def p2(data):
res = 0
M = { 'humn': sympy.Symbol('x') } # part 2 using sympy
lines = data.splitlines()
for line in lines:
m, job = line.split(': ')
if m in M:
continue
if job.isdigit():
M[m] = sympy.Integer(job) # sympy
else:
# print('job:', job)
left, do, right = job.split()
if left in M and right in M:
if m == 'root':
res = sympy.solve(M[left] - M[right])[0]
break
if do == '+':
M[m] = M[left] + M[right]
if do == '-':
M[m] = M[left] - M[right]
if do == '/':
M[m] = M[left] / M[right] # // throws err in sympy
if do == '*':
M[m] = M[left] * M[right]
else:
lines.append(line)
return res
# part 1
def p1(data):
M = {}
lines = data.splitlines()
for line in lines:
m, job = line.split(':')
job = job.split()
if type(job) is list and len(job) == 1:
job = int(job[0])
M[m] = job
res = calc(M['root'], M)
return res
def calc(job, M):
if type(job) is not int:
l, do, r = job
L = calc(M[l], M)
R = calc(M[r], M)
if do == '+':
return L + R
if do == '-':
return L - R
if do == '/':
return L // R
if do == '*':
return L * R
return job
if __name__ == '__main__':
main()