class TestIntcodeMachine(TestCase): def setUp(self): self.computer = IntcodeMachine() def test_addition(self): result = self.computer.run_program([1101, 100, -1, 4, 0]) self.assertEqual(result, 1101) def test_multiplication(self): result = self.computer.run_program([1002, 4, 3, 4, 33]) self.assertEqual(result, 1002) # Here's a larger example: # 3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31, # 1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104, # 999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99 # The above example program uses an input instruction to ask for a single number. # The program will then output 999 if the input value is below 8, output 1000 if the input value is equal to 8, # or output 1001 if the input value is greater than 8. def test_low_input(self): self.assertEqual(test_input_output(self.computer, 7), 999) def test_equal_input(self): self.assertEqual(test_input_output(self.computer, 8), 1000) def test_high_input(self): self.assertEqual(test_input_output(self.computer, 9), 1001) def test_load_program_from_file(self): computer = IntcodeMachine() computer.load_program_from_file('intcode_test_program.dat') self.assertEqual(computer.program, DEFAULT_PROGRAM)
def init_amplifiers(): global queues, computers queues = [ queue.Queue(), queue.Queue(), queue.Queue(), queue.Queue(), queue.Queue() ] computers = [ IntcodeMachine(), IntcodeMachine(), IntcodeMachine(), IntcodeMachine(), IntcodeMachine() ] for i in range(5): computers[i].set_queues(queues[i], queues[(i + 1) % 5])
def main(): program = IntcodeMachine.get_program_from_file('day_07.dat') init_amplifiers() phase_settings = [0, 1, 2, 3, 4] phase_permutations = list(itertools.permutations(phase_settings)) signal = compute_signal_strength(phase_permutations[0], program.copy()) for phase_array in phase_permutations[1:]: signal = max(signal, compute_signal_strength(phase_array, program.copy())) print(signal)
def test_65210(self): program = IntcodeMachine.get_program_from_file('amp_test_65210.dat') signal = compute_signal_strength([1, 0, 4, 3, 2], program) self.assertEqual(65210, signal)
def test_01234(self): program = IntcodeMachine.get_program_from_file('amp_test_01234.dat') signal = compute_signal_strength([0, 1, 2, 3, 4], program) self.assertEqual(54321, signal)
def test_load_program_from_file(self): computer = IntcodeMachine() computer.load_program_from_file('intcode_test_program.dat') self.assertEqual(computer.program, DEFAULT_PROGRAM)
def setUp(self): self.computer = IntcodeMachine()
def test_97856(self): program = IntcodeMachine.get_program_from_file( 'feedback_test_97856.dat') signal = compute_signal_strength([9, 7, 8, 5, 6], program) self.assertEqual(18216, signal)
def test_98765(self): program = IntcodeMachine.get_program_from_file( 'feedback_test_98765.dat') signal = compute_signal_strength([9, 8, 7, 6, 5], program) self.assertEqual(139629729, signal)