示例#1
0
 def test_run_executes_output_handler_on_opcode_4(self,
                                                  mock_parse_parameter,
                                                  mock_output_handler):
     mock_parse_parameter.side_effect = [(4, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_output_handler.called
示例#2
0
 def test_with_16_digit_number(self):
     memory = [1102, 34915192, 34915192, 7, 4, 7, 99, 0]
     computer = IntComputer(memory)
     computer.run()
     expected = 1219070632396864
     result = computer.output
     assert result == expected
示例#3
0
 def test_with_large_numbers(self):
     memory = [
         109,
         1,
         204,
         -1,
         1001,
         100,
         1,
         100,
         1008,
         100,
         16,
         101,
         1006,
         101,
         0,
         99,
     ]
     computer = IntComputer(memory, wait_after_output=True)
     all_outputs = []
     while not computer.finished:
         computer.run()
         all_outputs.append(computer.output)
     assert all_outputs[0:-1] == memory
示例#4
0
 def test_run_intcode_1(self):
     expected = 2
     memory = [1, 0, 0, 0, 99]
     computer = IntComputer(memory)
     computer.run()
     result = computer.memory[0]
     assert expected == result
示例#5
0
 def test_with_1125899906842624(self):
     memory = [104, 1125899906842624, 99]
     computer = IntComputer(memory)
     computer.run()
     expected = 1125899906842624
     result = computer.output
     assert result == expected
示例#6
0
 def test_run_does_nothing_when_finished(self, mock_parse_parameter,
                                         mock_add):
     mock_parse_parameter.return_value = (1, [])
     computer = IntComputer([])
     computer.finished = True
     computer.run()
     assert mock_add.called == False
示例#7
0
 def test_run_intcode_4(self):
     expected = 30
     memory = [1, 1, 1, 4, 99, 5, 6, 0, 99]
     computer = IntComputer(memory)
     computer.run()
     result = computer.memory[0]
     assert expected == result
示例#8
0
 def test_run_intcode_3(self):
     expected = 2
     memory = [2, 4, 4, 5, 99, 0]
     computer = IntComputer(memory)
     computer.run()
     result = computer.memory[0]
     assert expected == result
示例#9
0
    def get_map(cls, program: typing.List[int], draw_mode=False) -> "Map":
        computer = IntComputer(program, wait_after_output=True, wait_for_input=True)
        visited = defaultdict(int)
        droid_map = Map()
        steps = 0

        while not computer.finished and steps < 6000:
            direction = droid_map.get_best_direction(visited)
            steps += 1
            computer.inputs = [direction]
            computer.run()
            while not computer.waiting:
                computer.run()
            output = computer.output

            if output == OUTPUT_WALL:
                droid_map.wall_hit([NORTH, SOUTH, WEST, EAST][direction - 1])

            if output == OUTPUT_FREE:
                [droid_map.north, droid_map.south, droid_map.west, droid_map.east][
                    direction - 1
                ]()
                droid_map.set_position(droid_map.position, FREE)

            if output == OUTPUT_OXYGEN:
                [droid_map.north, droid_map.south, droid_map.west, droid_map.east][
                    direction - 1
                ]()
                droid_map.set_position(droid_map.position, OXYGEN)

            visited[droid_map.position] += 1
            if draw_mode:
                droid_map.render()

        return droid_map
示例#10
0
def find_gravity_assist_inputs(memory, desired_output):
    """Find noun and verb that gives desired_output when intcode is run."""
    for noun in range(0, 100):
        for verb in range(0, 100):
            computer = IntComputer(memory=memory[:], pointer=0)
            computer.run(noun=noun, verb=verb)
            output = computer.memory[0]
            if output == desired_output:
                return 100 * noun + verb
    return False
示例#11
0
def calculate_max_thruster(original_memory):
    res = 0
    all_phases = get_all_phases(0, 5)
    for phases in all_phases:
        second_input = 0
        for phase in phases:
            first_input = phase
            memory = original_memory.copy()
            computer = IntComputer(memory[:],
                                   inputs=[first_input, second_input],
                                   wait_after_output=True)
            computer.run()
            second_input = computer.output
        if second_input > res:
            res = second_input
    return res
示例#12
0
def day02_01():
    program = get_number_list_from_file("./puzzles/02/puzzle.txt")
    computer = IntComputer(memory=program[:], pointer=0)
    computer.run(noun=12, verb=2)
    print("Output prior to fire: {}".format(computer.memory[0]))
示例#13
0
 def test_run_executes_jump_if_false_on_opcode_6(self, mock_parse_parameter,
                                                 mock_jump_if_false):
     mock_parse_parameter.side_effect = [(6, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_jump_if_false.called == True
示例#14
0
 def test_run_executes_add_on_opcode_1(self, mock_parse_parameter,
                                       mock_add):
     mock_parse_parameter.side_effect = [(1, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_add.called == True
示例#15
0
 def test_run_finishes_on_opcode_99(self, mock_parse_parameter):
     mock_parse_parameter.return_value = (99, [])
     computer = IntComputer([])
     assert computer.finished == False
     computer.run()
     computer.finished = True
示例#16
0
 def test_run_executes_multiply_on_opcode_2(self, mock_parse_parameter,
                                            mock_multiply):
     mock_parse_parameter.side_effect = [(2, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_multiply.called == True
示例#17
0
 def test_run_executes_save_on_opcode_3(self, mock_parse_parameter,
                                        mock_save):
     mock_parse_parameter.side_effect = [(3, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_save.called == True
示例#18
0
 def test_run_executes_adjust_relative_base_on_opcode_8(
         self, mock_parse_parameter, mock_adjust_relative_base):
     mock_parse_parameter.side_effect = [(9, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_adjust_relative_base.called == True
示例#19
0
def day09_02():
    program = get_number_list_from_file("./puzzles/09/puzzle.txt")
    computer = IntComputer(memory=program, inputs=[2], wait_after_output=True)
    coordinates = computer.run()
    print(f"Coordinates {coordinates}")
示例#20
0
def day05_02():
    program = get_number_list_from_file("./puzzles/05/puzzle.txt")
    computer = IntComputer(memory=program)
    computer.inputs = [5]
    computer.run()
    print(f"thermal radiator controller (part 2) {computer.output}")
示例#21
0
def day09_01():
    program = get_number_list_from_file("./puzzles/09/puzzle.txt")
    computer = IntComputer(memory=program, inputs=[1], wait_after_output=True)
    boost_keycode = computer.run()
    print(f"BOOST keycode {boost_keycode}")
示例#22
0
 def test_run_executes_less_than_on_opcode_7(self, mock_parse_parameter,
                                             mock_less_than):
     mock_parse_parameter.side_effect = [(7, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_less_than.called == True
示例#23
0
def day05_01():
    program = get_number_list_from_file("./puzzles/05/puzzle.txt")
    computer = IntComputer(memory=program)
    computer.inputs = [1]
    computer.run()
    print(f"conditioner unit (part 1) {computer.output}")
示例#24
0
 def test_run_executes_equals_on_opcode_8(self, mock_parse_parameter,
                                          mock_equals):
     mock_parse_parameter.side_effect = [(8, []), (99, [])]
     computer = IntComputer([])
     computer.run()
     assert mock_equals.called == True