aoc2022/d21/part1.py

28 lines
547 B
Python

import sys
N = dict() # numbers
E = dict() # expressions
for l in sys.stdin.read().splitlines():
k, v = l.split(":")
v = v.strip()
if v.isdigit():
N[k]=int(v)
else:
w = v.split(" ")
E[k]=w
O = {
'*': lambda x,y: x*y,
'+': lambda x,y: x+y,
'-': lambda x,y: x-y,
'/': lambda x,y: x/y,
}
while len(E)>0:
D = dict()
for k,v in E.items():
(a,o,b) = v
if a in N and b in N:
N[k] = O[o](N[a],N[b])
else:
D[k] = v
E = D
print(N["root"])