|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, pathfinding, itertools |
| 3 | + |
| 4 | +from complex_utils import * |
| 5 | +from IntCode import * |
| 6 | + |
| 7 | +test_data = {} |
| 8 | + |
| 9 | +test = 1 |
| 10 | +test_data[test] = { |
| 11 | + "input": """3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0""", |
| 12 | + "expected": ["43210 (from phase setting sequence 4,3,2,1,0)", "Unknown"], |
| 13 | +} |
| 14 | + |
| 15 | +test += 1 |
| 16 | +test_data[test] = { |
| 17 | + "input": """3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0""", |
| 18 | + "expected": ["54321 (from phase setting sequence 0,1,2,3,4)", "Unknown"], |
| 19 | +} |
| 20 | + |
| 21 | +test += 1 |
| 22 | +test_data[test] = { |
| 23 | + "input": """3,52,1001,52,-5,52,3,53,1,52,56,54,1007,54,5,55,1005,55,26,1001,54, |
| 24 | +-5,54,1105,1,12,1,53,54,53,1008,54,0,55,1001,55,1,55,2,53,55,53,4, |
| 25 | +53,1001,56,-1,56,1005,56,6,99,0,0,0,0,10""", |
| 26 | + "expected": ["Unknown", "18216 (from phase setting sequence 9,7,8,5,6)"], |
| 27 | +} |
| 28 | + |
| 29 | +test = "real" |
| 30 | +input_file = os.path.join( |
| 31 | + os.path.dirname(__file__), |
| 32 | + "Inputs", |
| 33 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 34 | +) |
| 35 | +test_data[test] = { |
| 36 | + "input": open(input_file, "r+").read().strip(), |
| 37 | + "expected": ["929800", "15432220"], |
| 38 | +} |
| 39 | + |
| 40 | +# -------------------------------- Control program execution ------------------------- # |
| 41 | + |
| 42 | +case_to_test = "real" |
| 43 | +part_to_test = 2 |
| 44 | + |
| 45 | +# -------------------------------- Initialize some variables ------------------------- # |
| 46 | + |
| 47 | +puzzle_input = test_data[case_to_test]["input"] |
| 48 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 49 | +puzzle_actual_result = "Unknown" |
| 50 | + |
| 51 | + |
| 52 | +# -------------------------------- Actual code execution ----------------------------- # |
| 53 | + |
| 54 | +if part_to_test == 1: |
| 55 | + max_signal = 0 |
| 56 | + for settings in itertools.permutations("01234"): |
| 57 | + amplifiers = [IntCode(puzzle_input, i) for i in range(5)] |
| 58 | + for i in range(5): |
| 59 | + amplifiers[i].add_input(int(settings[i])) |
| 60 | + amplifiers[0].add_input(0) |
| 61 | + |
| 62 | + amplifiers[0].run() |
| 63 | + for i in range(1, 5): |
| 64 | + amplifiers[i].add_input(amplifiers[i - 1].outputs[-1]) |
| 65 | + amplifiers[i].run() |
| 66 | + |
| 67 | + max_signal = max(max_signal, amplifiers[4].outputs[-1]) |
| 68 | + |
| 69 | + puzzle_actual_result = max_signal |
| 70 | + |
| 71 | + |
| 72 | +else: |
| 73 | + max_signal = 0 |
| 74 | + for settings in itertools.permutations("56789"): |
| 75 | + amplifiers = [IntCode(puzzle_input, i) for i in range(5)] |
| 76 | + for i in range(5): |
| 77 | + amplifiers[i].add_input(int(settings[i])) |
| 78 | + amplifiers[0].add_input(0) |
| 79 | + |
| 80 | + while not all([x.state == "Stopped" for x in amplifiers]): |
| 81 | + for i in range(0, 5): |
| 82 | + if len(amplifiers[i - 1].outputs) > 0: |
| 83 | + amplifiers[i].add_input(amplifiers[i - 1].outputs) |
| 84 | + amplifiers[i - 1].outputs = [] |
| 85 | + amplifiers[i].restart() |
| 86 | + amplifiers[i].run() |
| 87 | + |
| 88 | + max_signal = max(max_signal, amplifiers[4].outputs[-1]) |
| 89 | + |
| 90 | + puzzle_actual_result = max_signal |
| 91 | + |
| 92 | + |
| 93 | +# -------------------------------- Outputs / results --------------------------------- # |
| 94 | + |
| 95 | +print("Expected result : " + str(puzzle_expected_result)) |
| 96 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments