def test_large_example(input_data, expected_output): # output 999, 1000, 1001 if the input value is respectively below, equal, or greater than 8 program = [ 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, ] output_stream = [] run_program(program, iter(input_data), output_stream) assert output_stream == expected_output
def test_jump_if_condition(self): for day05.INPUT in [x - 10 for x in range(30)]: program_list = [ 3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9 ] day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT != 0) for day05.INPUT in [x - 10 for x in range(30)]: program_list = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1] day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT != 0)
def test_input_output(self): program_list = [3, 0, 4, 0, 99] day05.INPUT = 42 day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT) program_list = [3, 0, 4, 0, 99] day05.INPUT = 1 day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT) program_list = [3, 0, 4, 0, 99] day05.INPUT = 2 self.assertEqual(day05.run_program(program_list, 0)[0], 4) self.assertEqual(day05.OUTPUT[-1], day05.INPUT)
def bigger_test(self): for day05.INPUT in range(20): program_list = [ 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 ] if day05.INPUT < 8: answer = 999 elif day05.INPUT == 8: answer = 1000 else: answer = 1001 day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], answer)
def test_run_program(self): program_list = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] answer = [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50] day05.run_program(program_list, 0) self.assertEqual(program_list, answer) program_list = [1, 0, 0, 0, 99] answer = [2, 0, 0, 0, 99] day05.run_program(program_list, 0) self.assertEqual(program_list, answer) program_list = [2, 3, 0, 3, 99] answer = [2, 3, 0, 6, 99] day05.run_program(program_list, 0) self.assertEqual(program_list, answer) program_list = [2, 4, 4, 5, 99, 0] answer = [2, 4, 4, 5, 99, 9801] day05.run_program(program_list, 0) self.assertEqual(program_list, answer) program_list = [1, 1, 1, 4, 99, 5, 6, 0, 99] answer = [30, 1, 1, 4, 2, 5, 6, 0, 99] day05.run_program(program_list, 0) self.assertEqual(program_list, answer)
def part1(content): max_output = 0 thruster = None for permu in itertools.permutations(range(5)): output = 0 for ph in permu: inputs = [ph, output] raw_intcode = day05.read_intcode_into_dict(content) (output, _, _) = day05.run_program(raw_intcode, inputs) if output > max_output: max_output = output thruster = permu print( f'Thruster signal is {max_output} with phases {"".join([str(x) for x in thruster])}' )
def part2(content): max_output = 0 thruster = None for permu in itertools.permutations(range(5, 10)): # load five amps amps = {} pointers = [] finished = [] for j, ph in enumerate(permu): intcode = day05.read_intcode_into_dict(content) # apply phase first (_, pointer, fin) = day05.run_program(intcode, [ph]) amps[j] = intcode pointers.append(pointer) finished.append(fin) # now that all amplifiers are loaded, the mayham can start... inputs = [0] j = 0 while False in finished: (output, pointer, done) = day05.run_program(amps[j], inputs, pointer=pointers[j]) pointers[j] = pointer finished[j] = done inputs[0] = output j += 1 if j == len(amps.keys()): j = 0 if output > max_output: max_output = output thruster = permu print( f'Thruster signal is {max_output} with phases {"".join([str(x) for x in thruster])}' )
def test_equals_less(self): for day05.INPUT in [x - 10 for x in range(30)]: program_list = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8] day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT == 8) for day05.INPUT in [x - 10 for x in range(30)]: program_list = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8] day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT < 8) for day05.INPUT in [x - 10 for x in range(30)]: program_list = [3, 3, 1108, -1, 8, 3, 4, 3, 99] day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT == 8) for day05.INPUT in [x - 10 for x in range(30)]: program_list = [3, 3, 1107, -1, 8, 3, 4, 3, 99] day05.run_program(program_list, 0) self.assertEqual(day05.OUTPUT[-1], day05.INPUT < 8)
def test_jump_programs(program, input_data, expected_output): output_stream = [] run_program(program, iter(input_data), output_stream) assert output_stream == expected_output
def test_output_programs(program, expected_output): output_list = [] assert run_program(program, None, output_list) == program assert output_list == expected_output
def test_input_programs(program, input_data, expected_output): assert run_program(program, iter(input_data), None) == expected_output
def test_sample_programs(program, expected_output): assert run_program(program, None, None) == expected_output