def amp_wrapper(prog): highest = -np.inf best = None exited = [False] * 5 amp_A = Amplifier("A", prog.copy(), [].copy()) amp_B = Amplifier("B", prog.copy(), [].copy()) amp_C = Amplifier("C", prog.copy(), [].copy()) amp_D = Amplifier("D", prog.copy(), [].copy()) amp_E = Amplifier("E", prog.copy(), [].copy()) for phase in list(itertools.permutations(list(range(5, 10)))): [a, b, c, d, e] = phase amp_A.inputs.append(a) amp_B.inputs.append(b) amp_C.inputs.append(c) amp_D.inputs.append(d) amp_E.inputs.append(e) exited = [False] * 5 e_out = 0 # initial input loopcount = 0 # debug(f"\nPhase input: {phase}\n") while (True): amp_A.inputs.append(e_out) amp_A.run_prog_from_current_state() a_out = amp_A.outputs[-1] amp_B.inputs.append(a_out) amp_B.run_prog_from_current_state() b_out = amp_B.outputs[-1] amp_C.inputs.append(b_out) amp_C.run_prog_from_current_state() c_out = amp_C.outputs[-1] amp_D.inputs.append(c_out) amp_D.run_prog_from_current_state() d_out = amp_D.outputs[-1] amp_E.inputs.append(d_out) amp_E.run_prog_from_current_state() e_out = amp_E.outputs[-1] if amp_E.has_exited: break thruster_signal = e_out if highest < thruster_signal: highest = thruster_signal best_phases = [a, b, c, d, e].copy() amp_A.reset() amp_B.reset() amp_C.reset() amp_D.reset() amp_E.reset() print("BEST PHASES: " + str(best_phases)) print("HIGHEST THRUSTER SIGNAL: " + str(highest))
if phase_b in [phase_a]: continue for phase_c in range(5): if phase_c in [phase_a, phase_b]: continue for phase_d in range(5): if phase_d in [phase_a, phase_b, phase_c]: continue for phase_e in range(5): if phase_e in [phase_a, phase_b, phase_c, phase_d]: continue amp_a.reset() amp_b.reset() amp_c.reset() amp_d.reset() amp_e.reset() amp_a.run([phase_a, 0]) amp_b.run([phase_b, amp_a.output()]) amp_c.run([phase_c, amp_b.output()]) amp_d.run([phase_d, amp_c.output()]) amp_e.run([phase_e, amp_d.output()]) if amp_e.output() > max_output: max_output = amp_e.output() print("Part 1:", max_output)