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.

57 lines
1.3 KiB
Python

with open("input") as f:
instructions = [n.strip() for n in f.readlines()]
solution_part_1 = 46065
SOURCES = dict()
for instruction in instructions:
print(instruction)
[operation, wire] = instruction.split(" -> ")
SOURCES[wire] = operation
SOURCES["b"] = solution_part_1
def evaluate_wire(wire):
try:
return int(wire)
except ValueError:
pass
operation = SOURCES[wire]
print("evaluating wire", wire, "; operation:", operation)
if type(operation) == int:
return 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"))
print("finish")