You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
3 years ago
|
with open("input") as f:
|
||
|
instructions = [n.strip() for n in f.readlines()]
|
||
|
|
||
|
SOURCES = dict()
|
||
|
|
||
|
for instruction in instructions:
|
||
|
print(instruction)
|
||
|
[operation, wire] = instruction.split(" -> ")
|
||
|
SOURCES[wire] = operation
|
||
|
|
||
|
print(SOURCES)
|
||
|
|
||
|
|
||
|
def evaluate_wire(wire):
|
||
|
try:
|
||
|
return int(wire)
|
||
|
except ValueError:
|
||
|
pass
|
||
|
|
||
|
operation = SOURCES[wire]
|
||
|
print("evaluating wire", wire, "; operation:", operation)
|
||
|
|
||
|
parameters = operation.split(" ")
|
||
|
|
||
|
result = None
|
||
|
|
||
|
if "NOT" in operation:
|
||
|
[_, a] = parameters
|
||
|
result = evaluate_wire(a) ^ 0b1111111111111111
|
||
|
elif "OR" in operation:
|
||
|
[a, _, b] = parameters
|
||
|
result = evaluate_wire(a) | evaluate_wire(b)
|
||
|
elif "AND" in operation:
|
||
|
[a, _, b] = parameters
|
||
|
result = evaluate_wire(a) & evaluate_wire(b)
|
||
|
elif "LSHIFT" in operation:
|
||
|
[a, _, b] = parameters
|
||
|
result = evaluate_wire(a) << evaluate_wire(b)
|
||
|
elif "RSHIFT" in operation:
|
||
|
[a, _, b] = parameters
|
||
|
result = evaluate_wire(a) >> evaluate_wire(b)
|
||
|
else:
|
||
|
result = evaluate_wire(operation)
|
||
|
|
||
|
SOURCES[wire] = str(result)
|
||
|
return result
|
||
|
|
||
|
|
||
|
print(evaluate_wire("a"))
|
||
|
# for wire in ["d", "e", "f", "g", "h", "i"]:
|
||
|
# print("==", wire, "==")
|
||
|
# print(evaluate_wire(wire))
|
||
|
# print(SOURCES)
|
||
|
|
||
|
print("finish")
|