Beispiel #1
0
def play(screen, program):
    if RENDER_GAME:
        curses.curs_set(0)
        y_max = 0
    computer = IntcodeComputer(program,
                               return_output=True,
                               return_before_input=True)
    current_score = 0
    game_tiles = {}
    while True:
        output = computer.run()
        if output is computer.sentinel_return:
            bx, px = ball_and_paddle_x(game_tiles)
            computer.append_inputs(-1 if bx < px else 1 if bx > px else 0)
            x = computer.run()
        else:
            x = output
        y = computer.run()
        tile_id = computer.run()
        if computer.halted():
            break
        if x == -1 and y == 0:
            current_score = tile_id
            if RENDER_GAME:
                screen.addstr(y_max + 2, 0, f"Score => {current_score}")
                screen.refresh()
        else:
            game_tiles[(x, y)] = tile_id
            if RENDER_GAME:
                y_max = max(y_max, y)
                screen.addstr(y, x, COMPONENTS[tile_id])
                screen.refresh()
                sleep(FRAME_RATE)
    return current_score
Beispiel #2
0
def painted_panels(intcode_program: list[int], starting_input: int,
                   get_image_meta: bool) -> dict[Position, int]:
    computer = IntcodeComputer(intcode_program,
                               inputs=[starting_input],
                               return_output=True)
    if get_image_meta:
        xmin = xmax = ymin = ymax = 0
    panels_painted = {}
    position: Position = (0, 0)
    pointing = "U"
    while True:
        color = computer.run()
        if computer.halted():
            break
        panels_painted[position] = color
        if get_image_meta:
            x, y = position
            xmin = min(xmin, x)
            xmax = max(xmax, x)
            ymin = min(ymin, y)
            ymax = max(ymax, y)
        direction = computer.run()
        position, pointing = move_robot(position, direction, pointing)
        computer.append_inputs(panels_painted.get(position, 0))
    if get_image_meta:
        return panels_painted, (xmin,
                                ymax), (abs(xmin) + xmax), (abs(ymin) + ymax)
    else:
        return panels_painted
Beispiel #3
0
def count_block_tiles(program):
    computer = IntcodeComputer(program, return_output=True)
    count = 0
    while True:
        computer.run()
        computer.run()
        tile_id = computer.run()
        if computer.halted():
            break
        if tile_id == 2:
            count += 1
    return count