1
0
Fork 0

Puzzle du 7 décembre 2015

This commit is contained in:
Lucidiot 2017-12-17 18:43:31 +01:00
parent 7e0dff3d0b
commit 960ef10877
No known key found for this signature in database
GPG Key ID: 63BD9482C29D0F64
1 changed files with 34 additions and 0 deletions

34
2015/7/wire.py Normal file
View File

@ -0,0 +1,34 @@
#!/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'))