示例#1
0
    def test_day7_1(self):

        for name, memory in [
            ('Day 7 test 1', self.DAY_7_MEMORY_1),
            ('Day 7 test 2', self.DAY_7_MEMORY_2),
            ('Day 7 test 3', self.DAY_7_MEMORY_3),
        ]:

            computer = IntcodeComputer(memory)
            best_phase_settings = None
            best_phase_output = -99999999
            initial_input = 0

            for phase_settings in list(permutations([0, 1, 2, 3, 4])):

                next_input = initial_input
                for phase in phase_settings:
                    computer.reset()
                    next_input = computer.run(phase, next_input)
                if next_input > best_phase_output:
                    best_phase_settings = phase_settings
                    best_phase_output = next_input

            with self.subTest(f'{name}'):
                expected_phase_setting, expected_outcome = self.DAY_7_EXPECTED_OUTCOMES[
                    name]
                self.assertEqual(best_phase_settings, expected_phase_setting)
                self.assertEqual(best_phase_output, expected_outcome)
def part1(puzzle_data):
    all_possible_phase_settings = list(permutations([0, 1, 2, 3, 4]))

    test_1 = [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]
    test_2 = [
        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
    ]
    test_3 = [
        3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33,
        1002, 33, 7, 33, 1, 33, 31, 31, 1, 32, 31, 31, 4, 31, 99, 0, 0, 0
    ]

    initial_input = 0

    for name, memory in [('test1', test_1), ('test2', test_2),
                         ('test3', test_3), ('puzzle', puzzle_data)]:

        computer = IntcodeComputer(memory)
        best_phase_settings = None
        best_phase_output = -99999999

        for phase_settings in all_possible_phase_settings:

            next_input = initial_input
            for phase in phase_settings:
                computer.reset()
                computer.run(phase, next_input)
                next_input = computer.output_values.pop()
            if next_input > best_phase_output:
                best_phase_settings = phase_settings
                best_phase_output = next_input

        print(name)
        print(best_phase_settings)
        print(best_phase_output)
        print('----')