コード例 #1
0
def amplifier(instructions, phases):
    input_val = 0
    for phase in phases:
        computer = Computer(instructions)
        computer.set_input(phase)
        input_val = computer.execute(input_val)
    return input_val
コード例 #2
0
ファイル: day_13.py プロジェクト: dhruvinmehta/advent-of-code
def block_tiles(instructions):
    count = 0
    output = []
    computer = Computer(instructions, True)
    while not computer.halted():
        output.append(computer.execute())
        if len(output) % 3 == 0:
            count += 1 if output[-1] == 2 else 0
    return count
コード例 #3
0
def registration_number(instructions):
    robot = Robot(Computer(instructions, True), 1)
    while not robot.computer.halted():
        robot.output.append(robot.computer.execute())
        robot.paint_panel()
    image = [[" "] * 50 for _ in range(6)]
    for panel in robot.panels:
        image[panel[0]][panel[1]] = '#' if robot.panels[panel] else ' '
    return image
コード例 #4
0
def feedback_amplifier(instructions, phases):
    computers = [Computer(instructions, True) for _ in range(5)]

    for index, phase in enumerate(phases):
        computers[index].set_input(phase)

    amp_index = 0
    input_val = 0
    for i in range(5):
        while not computers[i].halted():
            computers[amp_index].set_input(input_val)
            input_val = computers[amp_index].execute(input_val)
            amp_index = (amp_index + 1) % 5
    return input_val
コード例 #5
0
def create_area_map(instructions):
    queue = [(0, 0)]
    tile = ['#', '.', 'O']
    area_map = {(0, 0): '.'}
    visited = {(0, 0): Computer(instructions, True)}
    while queue:
        x, y = queue.pop(0)
        for move in DIRECTION:
            new_x, new_y = x + DIRECTION[move][0], y + DIRECTION[move][1]
            if (new_x, new_y) not in visited:
                computer = copy.deepcopy(visited[(x, y)])
                computer.set_input(move)
                output = computer.execute()
                area_map[(new_x, new_y)] = tile[output]
                visited[(new_x, new_y)] = computer
                if output != 0:
                    queue.append((new_x, new_y))
    return area_map
コード例 #6
0
ファイル: day_13.py プロジェクト: dhruvinmehta/advent-of-code
def score(instructions):
    instructions[0] = 2
    output = []
    game_score = 0
    paddle = ball = None
    computer = Computer(instructions, True)
    while not computer.halted():
        computer.set_input(max(-1, min(ball - paddle, 1)) if paddle and ball else 0, True)
        output.append(computer.execute(0))

        if len(output) % 3 == 0:
            paddle = output[-3] if output[-1] == 3 else paddle
            ball = output[-3] if output[-1] == 4 else ball
            game_score = output[-1] if (output[-3], output[-2]) == (-1, 0) else game_score
    return game_score
コード例 #7
0
def unique_painted_panels(instructions):
    robot = Robot(Computer(instructions, True))
    while not robot.computer.halted():
        robot.output.append(robot.computer.execute())
        robot.paint_panel()
    return len(robot.panels)
コード例 #8
0
def solution():
    print("Part 1 ans",
          Computer(read_input_file("Files/day-9-input.txt")).execute())
    print("Part 2 ans",
          Computer(read_input_file("Files/day-9-input.txt")).execute(2))
コード例 #9
0
# Problem Statement
# https://adventofcode.com/2019/day/9

from AoC_2019.Common.Computer import Computer


def read_input_file(file):
    with open(file) as file:
        return [int(element) for element in file.read().split(",")]


# Unit tests
assert len(str(Computer([1102, 34915192, 34915192, 7, 4, 7, 99,
                         0]).execute())) == 16
assert Computer([104, 1125899906842624, 99]).execute() == 1125899906842624


def solution():
    print("Part 1 ans",
          Computer(read_input_file("Files/day-9-input.txt")).execute())
    print("Part 2 ans",
          Computer(read_input_file("Files/day-9-input.txt")).execute(2))


solution()
コード例 #10
0
ファイル: day_5.py プロジェクト: dhruvinmehta/advent-of-code
# Problem Statement
# https://adventofcode.com/2019/day/5


from AoC_2019.Common.Computer import Computer


def read_input_file(file):
    with open(file) as file:
        return [int(element) for element in file.read().split(",")]


# Unit tests
assert Computer([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]).execute(8) == 1
assert Computer([3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]).execute(3) == 0
assert Computer([3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]).execute(5) == 1
assert Computer([3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]).execute(8) == 0
assert Computer([3, 3, 1108, -1, 8, 3, 4, 3, 99]).execute(8) == 1
assert Computer([3, 3, 1108, -1, 8, 3, 4, 3, 99]).execute(10) == 0
assert Computer([3, 3, 1107, -1, 8, 3, 4, 3, 99]).execute(6) == 1
assert Computer([3, 3, 1107, -1, 8, 3, 4, 3, 99]).execute(9) == 0
assert Computer([3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]).execute(0) == 0
assert Computer([3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]).execute(5) == 1
assert Computer([3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]).execute(0) == 0
assert Computer([3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]).execute(8) == 1


def solution():
    print("Part 1 ans", Computer(read_input_file("Files/day-5-input.txt")).execute())
    print("Part 2 ans", Computer(read_input_file("Files/day-5-input.txt")).execute(5))