from lib import get_data data = get_data(__file__) def run(wires={}): def get(a): try: return int(a) except ValueError: pass if a in wires: return wires[a] else: return None while "a" not in wires: for line in data.splitlines(): lhs, rhs = line.split(" -> ") if rhs in wires: continue match lhs.split(): case [a, "AND", b]: a, b = get(a), get(b) if a is not None and b is not None: wires[rhs] = a & b case [a, "OR", b]: a, b = get(a), get(b) if a is not None and b is not None: wires[rhs] = a | b case [a, "LSHIFT", b]: a, b = get(a), get(b) if a is not None and b is not None: wires[rhs] = a << b case [a, "RSHIFT", b]: a, b = get(a), get(b) if a is not None and b is not None: wires[rhs] = a >> b case ["NOT", a]: a = get(a) if a is not None: wires[rhs] = ~a & 0xFFFF case [a]: a = get(a) if a is not None: wires[rhs] = a return wires a = run()["a"] print(a) a2 = run(wires={"b": a})["a"] print(a2)