35 lines
1015 B
Python
Executable File
35 lines
1015 B
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
instructions = [l.strip().split(" -> ") for l in sys.stdin.readlines()]
|
|
operands = {i[1]: i[0].split(' ') for i in instructions}
|
|
wires = {}
|
|
|
|
|
|
def compute(wire):
|
|
try:
|
|
return int(wire)
|
|
except ValueError:
|
|
pass
|
|
|
|
if wire not in wires:
|
|
words = operands[wire]
|
|
if len(words) == 1:
|
|
wires[wire] = compute(words[0])
|
|
else:
|
|
if words[0] == "NOT":
|
|
wires[wire] = ~compute(words[1]) & 0xffff
|
|
elif words[1] == 'AND':
|
|
wires[wire] = compute(words[0]) & compute(words[2])
|
|
elif words[1] == 'OR':
|
|
wires[wire] = compute(words[0]) | compute(words[2])
|
|
elif words[1] == 'LSHIFT':
|
|
wires[wire] = (compute(words[0]) << compute(words[2]))
|
|
elif words[1] == 'RSHIFT':
|
|
wires[wire] = (compute(words[0]) >> compute(words[2]))
|
|
return wires[wire]
|
|
|
|
a = compute('a')
|
|
print(a)
|
|
wires = {'b': a}
|
|
print(compute('a'))
|