aoc2015/d07/run.py

39 lines
813 B
Python

import sys
R = dict() # registers
for l in sys.stdin.readlines():
v, k = l[:-1].split(' -> ')
R[k] = v
A = 2**16 -1 # FFFF
def val(k:str) -> int:
if k.isdigit():
return int(k)
v = R[k]
if type(v) == int: # k is resolved
return v
r = None
I = v.split(' ')
if len(I) == 1:
r = val(I[0])
else:
i = I[-2]
if i == "AND":
r = val(I[0]) & val(I[2])
elif i == "OR":
r = val(I[0]) | val(I[2])
elif i == "LSHIFT":
r = val(I[0]) << val(I[2])
elif i == "RSHIFT":
r = val(I[0]) >> val(I[2])
elif i == "RSHIFT":
r = val(I[0]) >> val(I[2])
elif i == "NOT":
r = ~ val(I[1])
r &= A
R[k] = r
return r
print(val(sys.argv[1]))