예제 #1
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
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
예제 #3
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
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
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)
def test_huge_number():
    program = Intcode([104, 1125899906842624, 99])
    program.run()
    assert program.get_output_history() == [1125899906842624]