|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, pathfinding |
| 3 | + |
| 4 | +from complex_utils import * |
| 5 | + |
| 6 | +test_data = {} |
| 7 | + |
| 8 | +test = 1 |
| 9 | +test_data[test] = { |
| 10 | + "input": """1,9,10,3,2,3,11,0,99,30,40,50""", |
| 11 | + "expected": ["3500,9,10,70,2,3,11,0,99,30,40,50", "Unknown"], |
| 12 | +} |
| 13 | + |
| 14 | +test += 1 |
| 15 | +test_data[test] = { |
| 16 | + "input": """1,0,0,0,99""", |
| 17 | + "expected": ["2,0,0,0,99", "Unknown"], |
| 18 | +} |
| 19 | + |
| 20 | +test += 1 |
| 21 | +test_data[test] = { |
| 22 | + "input": """2,4,4,5,99,0""", |
| 23 | + "expected": ["2,4,4,5,99,9801", "Unknown"], |
| 24 | +} |
| 25 | + |
| 26 | +test = "real" |
| 27 | +input_file = os.path.join( |
| 28 | + os.path.dirname(__file__), |
| 29 | + "Inputs", |
| 30 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 31 | +) |
| 32 | +test_data[test] = { |
| 33 | + "input": open(input_file, "r+").read().strip(), |
| 34 | + "expected": ["6327510", "4112"], |
| 35 | +} |
| 36 | + |
| 37 | +# -------------------------------- Control program execution ------------------------- # |
| 38 | + |
| 39 | +case_to_test = "real" |
| 40 | +part_to_test = 2 |
| 41 | +verbose_level = 2 |
| 42 | + |
| 43 | +# -------------------------------- Initialize some variables ------------------------- # |
| 44 | + |
| 45 | +puzzle_input = test_data[case_to_test]["input"] |
| 46 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 47 | +puzzle_actual_result = "Unknown" |
| 48 | + |
| 49 | + |
| 50 | +# -------------------------------- Actual code execution ----------------------------- # |
| 51 | + |
| 52 | + |
| 53 | +class IntCode: |
| 54 | + instructions = [] |
| 55 | + pointer = 0 |
| 56 | + state = "Running" |
| 57 | + |
| 58 | + def __init__(self, instructions): |
| 59 | + self.instructions = list(map(int, instructions.split(","))) |
| 60 | + |
| 61 | + def reset(self, instructions): |
| 62 | + self.instructions = list(map(int, instructions.split(","))) |
| 63 | + self.pointer = 0 |
| 64 | + self.state = "Running" |
| 65 | + |
| 66 | + def get_instruction(self): |
| 67 | + if self.instructions[self.pointer] in [1, 2]: |
| 68 | + return self.instructions[self.pointer : self.pointer + 4] |
| 69 | + else: |
| 70 | + return [self.instructions[self.pointer]] |
| 71 | + |
| 72 | + def op_1(self, instr): |
| 73 | + self.instructions[instr[3]] = ( |
| 74 | + self.instructions[instr[1]] + self.instructions[instr[2]] |
| 75 | + ) |
| 76 | + self.pointer += 4 |
| 77 | + self.state = "Running" |
| 78 | + |
| 79 | + def op_2(self, instr): |
| 80 | + self.instructions[instr[3]] = ( |
| 81 | + self.instructions[instr[1]] * self.instructions[instr[2]] |
| 82 | + ) |
| 83 | + self.pointer += 4 |
| 84 | + self.state = "Running" |
| 85 | + |
| 86 | + def op_99(self, instr): |
| 87 | + self.pointer += 1 |
| 88 | + self.state = "Stopped" |
| 89 | + |
| 90 | + def run(self): |
| 91 | + while self.state == "Running": |
| 92 | + current_instruction = self.get_instruction() |
| 93 | + getattr(self, "op_" + str(current_instruction[0]))(current_instruction) |
| 94 | + if verbose_level >= 3: |
| 95 | + print("Pointer after execution:", self.pointer) |
| 96 | + print("Instructions:", self.export()) |
| 97 | + |
| 98 | + def export(self): |
| 99 | + return ",".join(map(str, self.instructions)) |
| 100 | + |
| 101 | + |
| 102 | +if part_to_test == 1: |
| 103 | + computer = IntCode(puzzle_input) |
| 104 | + if case_to_test == "real": |
| 105 | + computer.instructions[1] = 12 |
| 106 | + computer.instructions[2] = 2 |
| 107 | + computer.run() |
| 108 | + puzzle_actual_result = computer.instructions[0] |
| 109 | + |
| 110 | + |
| 111 | +else: |
| 112 | + computer = IntCode(puzzle_input) |
| 113 | + for noon in range(100): |
| 114 | + for verb in range(100): |
| 115 | + computer.reset(puzzle_input) |
| 116 | + computer.instructions[1] = noon |
| 117 | + computer.instructions[2] = verb |
| 118 | + computer.run() |
| 119 | + if computer.instructions[0] == 19690720: |
| 120 | + puzzle_actual_result = 100 * noon + verb |
| 121 | + break |
| 122 | + |
| 123 | + if puzzle_actual_result != "Unknown": |
| 124 | + break |
| 125 | + |
| 126 | + |
| 127 | +# -------------------------------- Outputs / results --------------------------------- # |
| 128 | + |
| 129 | +print("Expected result : " + str(puzzle_expected_result)) |
| 130 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments