def test_intcode_day_two(self):
        program = [
            "1", "9", "10", "3", "2", "3", "11", "0", "99", "30", "40", "50"
        ]
        expected = [3500, 9, 10, 70, 2, 3, 11, 0, 99, 30, 40, 50]
        result = run_computer(program)[0]
        assert result == expected

        program = ["1", "0", "0", "0", "99"]
        expected = [2, 0, 0, 0, 99]
        result = run_computer(program)[0]
        assert result == expected

        program = ["2", "3", "0", "3", "99"]
        expected = [2, 3, 0, 6, 99]
        result = run_computer(program)[0]
        assert result == expected

        program = ["2", "4", "4", "5", "99", "0"]
        expected = [2, 4, 4, 5, 99, 9801]
        result = run_computer(program)[0]
        assert result == expected

        program = ["1", "1", "1", "4", "99", "5", "6", "0", "99"]
        expected = [30, 1, 1, 4, 2, 5, 6, 0, 99]
        result = run_computer(program)[0]
        assert result == expected
    def test_intcode_day_nine(self):
        program = [
            "109",
            "1",
            "204",
            "-1",
            "1001",
            "100",
            "1",
            "100",
            "1008",
            "100",
            "16",
            "101",
            "1006",
            "101",
            "0",
            "99",
        ]
        assert run_computer(program, [])[1] == list(map(int, program))

        program = ["1102", "34915192", "34915192", "7", "4", "7", "99", "0"]
        assert len(str(run_computer(program, [])[1][0])) == 16

        program = ["104", "1125899906842624", "99"]
        assert run_computer(program, [])[1][0] == 1125899906842624

        program = ["109", "6", "21001", "9", "25", "1", "104", "0", "99", "49"]
        assert run_computer(program, [])[1][0] == 74
Exemple #3
0
def run_feedback(program, sequence):
    next_input = 0
    previous_command = 0
    saved_position = [0] * len(sequence)
    saved_state = list(map(lambda s: copy(program), sequence))

    for i, phase in enumerate(sequence):
        saved_state[i], outputs, saved_position[i], _ = run_computer(
            saved_state[i], [phase, next_input],
            saved_position[i],
            feedback=True)
        next_input = outputs[0]

    while previous_command != 99:
        for i, _ in enumerate(sequence):
            saved_state[i], outputs, saved_position[i], _ = run_computer(
                saved_state[i], [next_input], saved_position[i], feedback=True)
            previous_command = saved_state[i][saved_position[i]]

            if len(outputs) == 0:
                break

            next_input = outputs[0]

    return next_input
Exemple #4
0
def map_panel(program, starting_colour):
    panel = {(0, 0): starting_colour}
    current_panel = (0, 0)
    current_direction = Direction.FORWARD
    op_position = 0
    relative_base = 0

    while True:
        input = [panel[current_panel]] if current_panel in panel else [0]

        program, outputs, op_position, relative_base = run_computer(
            program,
            input,
            op_position,
            relative_base,
            feedback=True,
            feedback_threshold=2)

        if len(outputs) == 0:
            break

        # Paint the current panel
        panel[current_panel] = outputs[0]

        # Figure out which way to turn
        current_direction = state_machine(current_direction, outputs[1])
        current_panel = move_robot(current_direction, current_panel)

    return panel
Exemple #5
0
def part_one(input):
    # Downloaded input is on one line
    input = input[0].split(',')

    input[1] = '12'
    input[2] = '2'

    result = run_computer(input)
    return result[0][0]
Exemple #6
0
def part_two(input):
    # Downloaded input is on one line
    input = input[0].split(',')

    for i in range(0, 100):
        for j in range(0, 100):
            input[1] = str(i)
            input[2] = str(j)
            if run_computer(input)[0][0] == 19690720:
                return 100 * i + j
Exemple #7
0
def part_two(input):
    program = input[0].split(",")
    program[0] = 2

    op_position = 0
    relative_base = 0
    joystick = 0

    score = 0

    grid = {}
    empty = []
    walls = []
    blocks = []
    paddle = (0, 0)

    while True:
        program, outputs, op_position, relative_base = run_computer(
            program,
            input=[joystick],
            initial_position=op_position,
            initial_relative_base=relative_base,
            feedback=True,
            feedback_threshold=3,
        )

        if len(outputs) == 0:
            break

        coordinates = (outputs[0], outputs[1])

        if coordinates == (-1, 0):
            score = outputs[2]
        else:
            grid[coordinates] = outputs[2]

            # Plotting the game
            value = grid[coordinates]
            if value == 0:
                empty.append(coordinates)
            elif value == 1:
                walls.append(coordinates)
            elif value == 2:
                blocks.append(coordinates)
            elif value == 3:
                paddle = coordinates

            joystick = check_if_destroyed_blocks(grid, coordinates, joystick, paddle)

    return score
Exemple #8
0
def part_one(input):
    program = input[0].split(",")

    op_position = 0
    relative_base = 0

    grid = {}

    while True:
        program, outputs, op_position, relative_base = run_computer(
            program,
            initial_position=op_position,
            initial_relative_base=relative_base,
            feedback=True,
            feedback_threshold=3,
        )

        if len(outputs) == 0:
            break

        coordinates = (outputs[0], outputs[1])
        grid[coordinates] = outputs[2]

    return [(k, len(list(v))) for k, v in groupby(sorted(grid.values()))][2][1]
    def test_intcode_day_five(self):
        program = ["3", "0", "4", "0", "99"]
        input = 25
        result = run_computer(program, [input])[1]
        assert result == [input]

        program = ["1002", "4", "3", "4", "33"]
        expected = [1002, 4, 3, 4, 99]
        result = run_computer(program)[0]
        assert result == expected

        program = ["1101", "100", "-1", "4", "0"]
        expected = [1101, 100, -1, 4, 99]
        result = run_computer(program)[0]
        assert result == expected

        program = [
            "3",
            "12",
            "6",
            "12",
            "15",
            "1",
            "13",
            "14",
            "13",
            "4",
            "13",
            "99",
            "-1",
            "0",
            "1",
            "9",
        ]
        assert run_computer(program, [0])[1][0] == 0
        assert run_computer(program, [8])[1][0] == 1

        program = [
            "3", "3", "1105", "-1", "9", "1101", "0", "0", "12", "4", "12",
            "99", "1"
        ]
        assert run_computer(program, [0])[1][0] == 0
        assert run_computer(program, [8])[1][0] == 1

        program = ["3", "9", "8", "9", "10", "9", "4", "9", "99", "-1", "8"]
        assert run_computer(program, [8])[1][0] == 1
        assert run_computer(program, [9])[1][0] == 0

        program = ["3", "9", "7", "9", "10", "9", "4", "9", "99", "-1", "8"]
        assert run_computer(program, [7])[1][0] == 1
        assert run_computer(program, [9])[1][0] == 0

        program = ["3", "3", "1108", "-1", "8", "3", "4", "3", "99"]
        assert run_computer(program, [8])[1][0] == 1
        assert run_computer(program, [9])[1][0] == 0

        program = ["3", "3", "1107", "-1", "8", "3", "4", "3", "99"]
        assert run_computer(program, [7])[1][0] == 1
        assert run_computer(program, [9])[1][0] == 0

        program = [
            "3",
            "21",
            "1008",
            "21",
            "8",
            "20",
            "1005",
            "20",
            "22",
            "107",
            "8",
            "21",
            "20",
            "1006",
            "20",
            "31",
            "1106",
            "0",
            "36",
            "98",
            "0",
            "0",
            "1002",
            "21",
            "125",
            "20",
            "4",
            "20",
            "1105",
            "1",
            "46",
            "104",
            "999",
            "1105",
            "1",
            "46",
            "1101",
            "1000",
            "1",
            "20",
            "4",
            "20",
            "1105",
            "1",
            "46",
            "98",
            "99",
        ]
        assert run_computer(program, [7])[1][0] == 999
        assert run_computer(program, [8])[1][0] == 1000
        assert run_computer(program, [9])[1][0] == 1001
Exemple #10
0
def part_two(input):
    program = input[0].split(",")

    return run_computer(program, [2])[1][0]
Exemple #11
0
def part_one(input):
    # Downloaded input is on one line
    program = input[0].split(',')

    return list(filter(bool, run_computer(program, [1])[1]))[0]
Exemple #12
0
def run_sequence(program, sequence):
    next_input = 0
    for phase in sequence:
        next_input = run_computer(copy(program), [phase, next_input])[1][0]

    return next_input