from lib import get_data, str_to_ints from d9 import Amp xs = str_to_ints(get_data(__file__)) first_255 = True nat = None y_prev = None amps = [] for i in range(50): a = Amp(xs) a.feed(i) a.go() amps.append(a) for j in range(1_000_000): was_active = False for i, a in enumerate(amps): a.go() if a.input_required: a.feed(-1) if len(a.outputs) > 0: was_active = True while len(a.outputs) < 3: a.go() addr = a.pop() x = a.pop() y = a.pop() if addr == 255: nat = (x, y) if first_255: print(y) first_255 = False else: amps[addr].feed(x) amps[addr].feed(y) if not was_active and nat is not None: x, y = nat if y == y_prev: print(y) exit() amps[0].feed(x) amps[0].feed(y) nat = None y_prev = y