Ejemplo n.º 1
0
def test_run_intcode(initial: List, expected: List) -> None:
    """
    Run a sample Intcode program and verify the final state of the program after calling halt.
    """
    program = Intcode(initial)
    program.run()
    assert program.get_program() == expected
Ejemplo n.º 2
0
def part2(data):
    cpu = Intcode(debug=False, wfi_mode=True)
    cpu.load_program(data)
    cpu.ram[0] = 2
    game = Game(cpu)
    game.start()
    return game.get_score()
def test_quine():
    initial = [
        109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0,
        99
    ]
    program = Intcode(copy.deepcopy(initial))
    program.run()
    assert program.get_output_history() == initial
Ejemplo n.º 4
0
def first_star() -> int:
    """
    Load and run the provided intcode program, then return the value in index 0.
    """
    initial = _get_initial_list()
    initial[1] = 12
    initial[2] = 2
    program = Intcode(initial)
    program.run()
    return program.get_program()[0]
Ejemplo n.º 5
0
def _get_thruster_signal_once(program: List[int],
                              phase_sequence: Tuple[int]) -> int:
    """
    Run through all of the five amplifiers in sequence. Take the output of the final amplifier
    (which would have been the "input value" of a sixth hypothetical amplifier).
    """
    input_value = 0
    for ps in phase_sequence:
        computer = Intcode(copy.deepcopy(program), [ps, input_value])
        computer.run()
        input_value = computer.get_output_history()[0]
    return input_value
Ejemplo n.º 6
0
def run(starting_value: int) -> Intcode:
    """
    Run the paint robot. Provide a starting value of either BLACK or WHITE.
    """
    program = Intcode(
        pathlib.Path("fixtures/day11_input1.txt").read_text(),
        [starting_value])
    squares = {}
    current_square = (0, 0)
    direction_index = 0
    while not program.halted():
        program.run(suppress_output=True)
        color, turn = program.get_output_history()[-2:]  # pylint: disable=unbalanced-tuple-unpacking
        squares[current_square] = color
        if turn == TURN_LEFT:
            direction_index = (direction_index + 1) % 4
        elif turn == TURN_RIGHT:
            direction_index = (direction_index - 1) % 4
        else:
            raise ValueError("What kind of direction is that")
        new_x = current_square[0] + CYCLE[direction_index][0]
        new_y = current_square[1] + CYCLE[direction_index][1]
        current_square = (new_x, new_y)
        program.push_input(squares.get(current_square, DEFAULT_COLOR))
    return squares
Ejemplo n.º 7
0
def second_star(target: int) -> int:
    """
    Find the "noun" and "verb" - integers from 0-99 - to replace input indices 1 and 2 - that when
    running the program, produces the target value in index 0.
    Load list from file once; copy as needed to iterate over different input parameters.
    """
    initial = _get_initial_list()
    for noun in range(0, 100):
        for verb in range(0, 100):
            modified = copy.deepcopy(initial)
            modified[1] = noun
            modified[2] = verb
            program = Intcode(modified)
            program.run()
            if program.get_program()[0] == target:
                return noun * 100 + verb
    raise RuntimeError("Can't find suitable inputs")
Ejemplo n.º 8
0
def part1(data):
    cpu = Intcode()
    cpu.load_program(data)
    cpu.run()

    return len([
        tile for i, tile in enumerate(cpu.outputs) if (i % 3, tile) == (2, 2)
    ])
Ejemplo n.º 9
0
def _get_thruster_signal_loop(program: List[int],
                              phase_sequence: Tuple[int]) -> int:
    """
    Loop through all amplifiers in a "round robin" style. Introduces the possibliity that a
    running Intcode program can exhaust its input stack, blocking until more input is given.

    As before - inputs from the previous amplifier are fed into the next until all programs have
    formally halted.
    """
    input_value = 0
    count = 0
    programs = [Intcode(copy.deepcopy(program), [ps]) for ps in phase_sequence]
    halted = [program.halted() for program in programs]
    while True:
        index = count % 5
        program = programs[index]
        program.push_input(input_value)
        program.run()
        halted[index] = program.halted()
        input_value = program.get_output_history()[-1]
        count += 1
        if all(halted):
            return input_value
Ejemplo n.º 10
0
def test_input_Poutput(code: List[int], input_: List[int],
                       expected: List[int]):
    program = Intcode(code, input_)
    program.run()
    assert program.get_output_history() == expected
Ejemplo n.º 11
0
def test_negative_values():
    program = Intcode([1101, 100, -1, 4, 0])
    program.run()
    assert program.get_program() == [1101, 100, -1, 4, 99]
Ejemplo n.º 12
0
def second_star() -> None:
    program = Intcode(_get_initial_list(), [5])
    program.run()
Ejemplo n.º 13
0
def test_16_digits():
    program = Intcode([1102, 34915192, 34915192, 7, 4, 7, 99, 0])
    program.run()
    assert (10**15) < program.get_output_history()[0] < (10**16)
Ejemplo n.º 14
0
def test_huge_number():
    program = Intcode([104, 1125899906842624, 99])
    program.run()
    assert program.get_output_history() == [1125899906842624]
Ejemplo n.º 15
0
def main():
    initial = pathlib.Path("fixtures/day9_input1.txt").read_text()
    program = Intcode(initial, [1])
    program.run()
    program = Intcode(initial, [2])
    program.run()
Ejemplo n.º 16
0
def test_intcode_input():
    input_ = random.randint(0, 100)
    program = Intcode([3, 0, 4, 0, 99], [input_])
    program.run()
    assert program.get_program() == [input_, 0, 4, 0, 99]
Ejemplo n.º 17
0
def test_multiplication_parameter_modes():
    program = Intcode([1002, 4, 3, 4, 33])
    program.run()
    assert program.get_program() == [1002, 4, 3, 4, 99]
Ejemplo n.º 18
0
def first_star() -> None:
    program = Intcode(_get_initial_list(), [1])
    program.run()