示例#1
0
    def test_addition_opcode(self):
        prog = IntcodeComputer()
        sample_op = [1, 0, 0, 0, 99]
        expectation = [2, 0, 0, 0, 99]

        result = prog.compute_sequence(sample_op)
        self.assertEqual(result, expectation)
示例#2
0
def runRobot(instructions):
    robotController = getInputData()
    IC = IntcodeComputer(robotController)
    IC.run_program(instructions)
    outCode = IC._output
    # print(ascii2str(outCode))
    return ascii2str(outCode)
示例#3
0
def day07PartTwo():
    thrusterDict = {}
    amplifierController = getInputData()
    for seq in itertools.permutations([5, 6, 7, 8, 9], 5):
        print(f'----- {seq} -----')
        ICA = IntcodeComputer(amplifierController)
        ICB = IntcodeComputer(amplifierController)
        ICC = IntcodeComputer(amplifierController)
        ICD = IntcodeComputer(amplifierController)
        ICE = IntcodeComputer(amplifierController)
        signal = runOneSeq(ICA, ICB, ICC, ICD, ICE, seq)
        print(f'-  {signal}  -')
        thrusterDict[seq] = signal

    maxSetting = max(thrusterDict, key=thrusterDict.get)
    maxSignal = max(thrusterDict.values())
    print(thrusterDict)
    print(maxSetting)
    print(maxSignal)

    print(
        f'Solution Day 07, Part two:\nAnswer: Amp.dettings:{maxSetting} --> signal:{maxSignal},  \n\n'
    )

    return signal
示例#4
0
def paint_ship(program, start_colour):
    location = (0, 0)
    direction = 0

    panels = {}

    panels[location] = start_colour

    computer = IntcodeComputer(program)

    program_output = computer.run_program()

    while True:
        current_panel = panels.setdefault(location)
        computer.add_input(0 if current_panel is None else current_panel)
        try:
            colour = next(program_output)
            turn = next(program_output)
        except StopIteration:
            break
        panels[location] = colour
        direction = (direction + turn*2 - 1) % 4
        location = (location[0] + DIRECTIONS[direction][0],
                    location[1] + DIRECTIONS[direction][1])

    return panels
示例#5
0
def part2(amp_controller_code: List[int]) -> int:
    output_signals = []
    input_a = 0
    for phase_setting_sequence in permutations(range(5, 10), 5):
        a, b, c, d, e = phase_setting_sequence
        amp_computers = [
            IntcodeComputer(amp_controller_code, inputs=[a]),  # amp a
            IntcodeComputer(amp_controller_code, inputs=[b]),  # amp b
            IntcodeComputer(amp_controller_code, inputs=[c]),  # amp c
            IntcodeComputer(amp_controller_code, inputs=[d]),  # amp d
            IntcodeComputer(amp_controller_code, inputs=[e])   # amp e
        ]
        output = 0
        while True:
            return_codes = []
            for amp_comp in amp_computers:
                amp_comp.set_inputs([output])
                return_code, return_value = amp_comp.execute()
                if return_code == IntcodeComputer.RETURNING_OUTPUT:
                    output = return_value
                return_codes.append(return_code)
            if [IntcodeComputer.HALT] * 5 == return_codes:
                break

        output_signals.append(output)

    return max(output_signals)
示例#6
0
def collect_dust(memory, main, functions, continuous_video_feed=False):
    memory[0] = 2

    inputs = main + functions[0] + functions[1] + functions[2] + [121 if continuous_video_feed else 110, 10]
    intcode_computer = IntcodeComputer(memory, initial_inputs=inputs)
    intcode_computer.run()
    return intcode_computer.get_all_outputs()
示例#7
0
def day05PartOne():
    prg = getInputData()
    IntCode = IntcodeComputer(prg)
    print(
        f'Solution Day 05, Part one:\nRunning TEST diagnostic program...\nGive manual input=>1\n'
    )
    IntCode.run_program()
示例#8
0
    def test_addition_and_multiplication_opcode(self):
        prog = IntcodeComputer()
        sample_op = [1, 1, 1, 4, 99, 5, 6, 0, 99]
        expectation = [30, 1, 1, 4, 2, 5, 6, 0, 99]

        result = prog.compute_sequence(sample_op)
        self.assertEqual(result, expectation)
示例#9
0
def day05PartTwo():
    prg2 = getInputData()
    IntCode2 = IntcodeComputer(prg2)
    print(
        f'Solution Day 05, Part two:\nRunning TEST diagnostic program...\nGive manual input=>5\n'
    )
    IntCode2.run_program()
示例#10
0
    def test_multiplication_opcode(self):
        prog = IntcodeComputer()
        sample_op = [2, 3, 0, 3, 99]
        expectation = [2, 3, 0, 6, 99]

        result = prog.compute_sequence(sample_op)
        self.assertEqual(result, expectation)
示例#11
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)
示例#12
0
def part1(program: List[int]) -> int:
    computer = IntcodeComputer(program, [1])

    while True:
        code, result = computer.execute()
        if code == IntcodeComputer.RETURNING_OUTPUT:
            return result
示例#13
0
 def test_day9_1(self):
     data = {
         "day 9 part 1 - 1": {
             'memory': [
                 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101,
                 1006, 101, 0, 99
             ],
             'expected_output': [
                 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101,
                 1006, 101, 0, 99
             ]
         },
         "day 9 part 1 - 2": {
             'memory': [1102, 34915192, 34915192, 7, 4, 7, 99, 0],
             'expected_output': 1219070632396864
         },
         "day 9 part 1 - 3": {
             'memory': [104, 1125899906842624, 99],
             'expected_output': 1125899906842624
         }
     }
     for name in data.keys():
         computer = IntcodeComputer(data[name]['memory'])
         test_output = computer.run()
         expected_output = data[name]['expected_output']
         with self.subTest(name):
             self.assertEqual(expected_output, test_output)
示例#14
0
def paint_hull(paint_bot: IntcodeComputer, ship_hull: Hull) -> Hull:
    current_location = (0, 0)
    heading = 0
    while True:
        try:
            bot_input = ship_hull[current_location]
        except KeyError:
            bot_input = 0
        paint_bot.set_inputs([bot_input])

        code, color = paint_bot.execute()
        if code != IntcodeComputer.RETURNING_OUTPUT:
            break

        ship_hull[current_location] = color

        code, turn_direction = paint_bot.execute()
        if code != IntcodeComputer.RETURNING_OUTPUT:
            raise Exception("Should not be exiting here.")

        heading = (heading + ROTATIONS[turn_direction]) % len(DIRECTIONS)
        x, y, = current_location
        dx, dy = DIRECTIONS[heading]
        current_location = x + dx, y + dy

    return ship_hull
示例#15
0
def paint_hull(program, start_color):
    computer = IntcodeComputer(program)
    # NESW
    direction = 0
    position = (0, 0)
    panel_colors = defaultdict(int)
    panel_colors[position] = start_color
    panels_painted = 1

    def new_direction(turn_right: int):
        if turn_right:
            return (direction + 1) % 4
        else:
            return (direction - 1) % 4

    while not computer.run(panel_colors[position]):
        paint_color = computer.outputs[-2]
        turn_right = computer.outputs[-1]
        panel_colors[position] = paint_color
        direction = new_direction(turn_right)
        position = update_position(position, direction)
        if position not in panel_colors:
            panels_painted += 1

    return panels_painted, panel_colors
示例#16
0
def cameraOutput(defaultColor=0):
    code = getInputData()
    IC = IntcodeComputer(code)

    step = 0
    inp = []
    IC._output = []  # Clear the output list
    terminate = False
    stoppedAtInput = False
    while not terminate:
        while not stoppedAtInput and not terminate:
            terminate, stoppedAtInput = IC.perform_one_operation(
                IC._memoryPosition, inp, stopAtInput=True)
        step += 1
        # print(f'### STEP {step} ###')
        # print(f'OutPut{IC._output}')
        # print(f'OutPut{IC._output}')
        stoppedAtInput = False

    ret = decodeMap(IC._output)
    # ret = []
    # row = ''
    # for ascii in IC._output:
    #     if str(ascii) == '10':
    #         ret.append(row)
    #         row = ''
    #     else:
    #         row += chr(ascii)
    return ret
示例#17
0
    def test_another_multiplication_opcode(self):
        prog = IntcodeComputer()
        sample_op = [2, 4, 4, 5, 99, 0]
        expectation = [2, 4, 4, 5, 99, 9801]

        result = prog.compute_sequence(sample_op)
        self.assertEqual(result, expectation)
示例#18
0
def find_noun_verb_pair():
    for noun in range(100):
        for verb in range(100):
            code = get_code(noun, verb)
            computer = IntcodeComputer()
            computer.execute_program(code)
            if code[0] == 19690720:
                return noun, verb
def is_tractor_beamed(coord):
  computer = IntcodeComputer(intcode[:], [])
  computer.inputs.append(coord[1])
  computer.inputs.append(coord[0])
  current_outputs = computer.outputs[:]
  while computer.outputs == current_outputs and not computer.halted:
    computer.perform_next_operation()
  return computer.outputs[-1]
示例#20
0
	def __init__(self, file=None, echo=True):
		if file is None:
			self.computer = IntcodeComputer(echo=echo)
		else:
			self.computer = IntcodeComputer(file, echo=echo)

		self.width, self.height = 0, 0
		self.grid = []
示例#21
0
def isInBeam(x, y):
    code = getInputData()
    IC = IntcodeComputer(code)
    o = IC.run_program([x, y])
    if o[0] == 1:
        return True
    else:
        return False
示例#22
0
def test_jump_immediate():
    intcode_list = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]
    inputs = [0, 8, 10, -5]
    expected_outputs = [0, 1, 1, 1]
    for input_value, expected_output in zip(inputs, expected_outputs):
        computer = IntcodeComputer(intcode_list)
        for value in computer.execute(input_value):
            pass
        assert value == expected_output
示例#23
0
def test_jump_position():
    intcode_list = [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]
    inputs = [0, 8, 10, -5]
    expected_outputs = [0, 1, 1, 1]
    for input_value, expected_output in zip(inputs, expected_outputs):
        computer = IntcodeComputer(intcode_list)
        for value in computer.execute(input_value):
            pass
        assert value == expected_output
示例#24
0
def test_less_than_position_mode():
    intcode_list = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]
    inputs = [0, 8, 10]
    expected_outputs = [1, 0, 0]
    for input_value, expected_output in zip(inputs, expected_outputs):
        computer = IntcodeComputer(intcode_list)
        for value in computer.execute(input_value):
            pass
        assert value == expected_output
示例#25
0
def test_less_than_immediate_mode():
    intcode_list = [3, 3, 1107, -1, 8, 3, 4, 3, 99]
    inputs = [0, 8, 10]
    expected_outputs = [1, 0, 0]
    for input_value, expected_output in zip(inputs, expected_outputs):
        computer = IntcodeComputer(intcode_list)
        for value in computer.execute(input_value):
            pass
        assert value == expected_output
示例#26
0
    def __init__(self, file_str, echo=True):
        self.computer = IntcodeComputer(file_str, echo=False)
        self.x, self.y = 0, 0
        self.min_x, self.max_x = 0, 0
        self.min_y, self.max_y = 0, 0

        self.map = {(self.x, self.y): '.'}
        self.path = []
        self.target = None
示例#27
0
def get_linear_system_output(phase_settings, memory):
    last_output = 0
    for phase_setting in phase_settings:
        inputs = [phase_setting, last_output]
        intcode_computer = IntcodeComputer(memory, inputs)
        intcode_computer.run()
        last_output = intcode_computer.pop_last_output()

    return last_output
示例#28
0
def run_the_magic_program(input_sequence, noun, verb):
    # create a copy of input_sequence to make sure we don't change the
    # original one
    input_sequence_copy = input_sequence.copy()
    input_sequence_copy[1] = noun
    input_sequence_copy[2] = verb

    command_invoker = IntcodeComputer(program=input_sequence_copy)
    command_invoker.run_program()
    return command_invoker.get_memory_state()[0]
示例#29
0
    def __init__(self, input_file=None, echo=True):
        if input_file:
            self.computer = IntcodeComputer(input_file, echo=echo)
        else:
            self.computer = IntcodeComputer(echo=echo)

        # 0 UP, 1 RIGHT, 2 DOWN, 3 LEFT
        self.direction = 0
        self.position = (0, 0)
        self.painted_tiles = {}
        self.echo = echo
示例#30
0
def part1(amp_controller_code: List[int]) -> int:
    output_signals = []
    for phase_setting_sequence in permutations(range(5), 5):
        amp_output = 0
        for setting in phase_setting_sequence:
            computer = IntcodeComputer(amp_controller_code,
                                       inputs=[setting, amp_output])
            code, amp_output = computer.execute()
        output_signals.append(amp_output)

    return max(output_signals)