def move(codes, is_origin_white=False):
    visited = set()
    white_pos = set()
    pos = (0, 0)  # (y, x)
    direction = (-1, 0)  # (y, x): face up

    if is_origin_white:
        white_pos.add(pos)

    intcode = Intcode(0, codes)

    while not intcode.halted():
        inst = [1] if pos in white_pos else [0]
        color, turn = intcode.run(2, inst).get_output()[-2:]

        visited.add(pos)
        if color == 1:
            white_pos.add(pos)
        elif pos in white_pos:
            white_pos.remove(pos)

        direction = right(direction[0], direction[1]) if turn == 1 else left(
            direction[0], direction[1])
        pos = sum(i[0]
                  for i in [pos, direction]), sum(i[1]
                                                  for i in [pos, direction])

    return visited, white_pos
예제 #2
0
    if tile_id == 2:  # block tile
        blocks.add((x, y))
    elif tile_id == 3:  # paddle tile
        paddle_x = x
    elif tile_id == 4:  # ball tile
        ball_x = x
        if (x, y) in blocks:
            blocks.remove((x, y))
    elif tile_id == 0:  # empty tile
        if (x, y) in blocks:
            blocks.remove((x, y))


if __name__ == '__main__':
    intcode = Intcode(0, puzzle_input)

    while not intcode.halted():
        parse(intcode.run(times=3).get_output())

    # Part 1
    print(len(blocks))

    # Part 2
    puzzle_input[0] = 2
    intcode = Intcode(1, puzzle_input)

    while not intcode.halted() or not len(blocks) == 0:
        inst = 1 if paddle_x < ball_x else -1 if paddle_x > ball_x else 0
        parse(intcode.run(3, [inst]).get_output())
    print(score)
예제 #3
0
    36, 1, 1, 1, 3, 1, 36, 1, 1, 1, 3, 1, 36, 1, 1, 1, 3, 1, 36, 1, 1, 1, 3, 1,
    36, 1, 1, 1, 3, 1, 28, 9, 1, 1, 3, 1, 28, 1, 9, 1, 3, 1, 16, 9, 3, 1, 5, 9,
    16, 1, 7, 1, 3, 1, 5, 1, 3, 1, 20, 1, 7, 1, 1, 13, 20, 1, 7, 1, 1, 1, 1, 1,
    5, 1, 24, 1, 7, 1, 1, 1, 1, 1, 5, 1, 24, 1, 7, 1, 1, 1, 1, 1, 5, 1, 24, 1,
    7, 1, 1, 1, 1, 1, 5, 1, 24, 1, 7, 1, 1, 1, 1, 1, 5, 1, 24, 13, 5, 1, 32, 1,
    1, 1, 7, 1, 20, 13, 1, 1, 7, 1, 20, 1, 13, 1, 7, 1, 20, 1, 13, 9, 20, 1,
    42, 1, 1, 9, 32, 1, 1, 1, 40, 1, 1, 1, 40, 1, 1, 1, 40, 1, 1, 1, 40, 1, 1,
    1, 40, 1, 1, 1, 40, 1, 1, 1, 40, 11, 34, 1, 7, 1, 34, 1, 7, 1, 34, 1, 7, 1,
    34, 11, 40, 1, 1, 1, 40, 1, 1, 1, 40, 1, 1, 1, 40, 13, 32, 1, 9, 1, 32, 1,
    9, 1, 32, 1, 9, 1, 32, 1, 9, 1, 32, 1, 9, 1, 32, 1, 9, 1, 32, 1, 9, 1, 32,
    11, 20
]

if __name__ == '__main__':
    intcode = Intcode(0, puzzle_input)
    while not intcode.halted():
        intcode.run()

    img = []
    line = []
    for x in intcode.get_output():
        if x == 10:
            img.append(line)
            line = []
        else:
            line.append(chr(x))

    scaffold = []
    sum = 0
    for y, row in enumerate(img):
        for x, cell, in enumerate(img[y]):