aoc2022/d21/part2.py

44 lines
973 B
Python
Raw Normal View History

import sys
N = dict() # numbers
E = list() # expressions
I = {"+":"-","-":"+","*":"/","/":"*"}
for l in sys.stdin.read().splitlines():
k, v = l.split(":")
v = v.strip()
if v.isdigit():
N[k]=int(v)
else:
(a,o,b) = v.split(" ")
E.append((k,a,o,b))
if o in {"+","*"}:
E.append((a,k,I[o],b))
E.append((b,k,I[o],a))
else:
E.append((a,k,I[o],b))
E.append((b,a,o,k))
if len(N)<100:
N["pppw"] = 150
else:
N["qmfl"] = 54426117311903
N["qdpj"] = 54426117311903
del N["humn"]
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 = list()
for (k,a,o,b) in E:
if a in N and b in N:
N[k] = O[o](N[a],N[b])
if k == "humn":
print(N[k])
D = {}
break
else:
D.append((k,a,o,b))
E = D