Example #1
0
def survey_hull(memory:list) -> int:
    program = IntcodeComputer(memory)
    input = deque(create_input())
    out = []
    program.execute(input, out)
    print_output(out)
    return out[-1]
Example #2
0
def move_robot(memory: list, path: list, video_feed: bool = False) -> int:
    memory[0] = 2
    program = IntcodeComputer(memory)
    routines = convert_path_to_routines(path)
    routine_sequence = find_routine_sequence(path, routines)
    ascii_input = create_input(routines, routine_sequence, video_feed)
    out = []
    program.execute(deque(ascii_input), out)
    out_str = "".join(map(chr, out))
    return out[-1]
Example #3
0
def scan_area(memory:list, x_lim:int, y_lim:int) -> Union[list, int]:
    program = IntcodeComputer(memory)
    area = [['.']*x_lim for __ in range(y_lim)]
    affected = 0
    for x, y in product(range(x_lim), range(y_lim)):
        out = []
        program.execute(deque([x,y]), out)
        program.reset()
        if out[-1] == 1:
            area[y][x] = '#'
            affected += 1
    return area, affected
Example #4
0
def play_game(memory: list,
              animate: bool = False,
              refresh_rate: int = 20) -> int:
    memory[0] = 2
    program = IntcodeComputer(memory)

    # set up the initial game board
    out, tiles, height, width = init_board(program)
    score = update(out, tiles)
    block_count, ball, paddle = get_game_info(tiles)
    input = deque([move_joystick(ball, paddle)])
    if animate:
        animate_game(out, tiles, height, width, score)

    # game is played until all blocks are gone
    while block_count > 0:
        out = []
        res = program.execute(input, out)
        out = [out[i:i + 3] for i in range(0, len(out), 3)]
        tmp_score = update(out, tiles)
        if tmp_score:
            score = tmp_score
        block_count, ball, paddle = get_game_info(tiles)
        input.append(move_joystick(ball, paddle))
        if animate:
            animate_game(out, tiles, height, width, score, True)
            sleep(1 / refresh_rate)
    return score
Example #5
0
def init_board(program: IntcodeComputer) -> Union[list, list, int, int]:
    out = []
    res = program.execute([], out)
    out = [out[i:i + 3] for i in range(0, len(out), 3)]
    height = max(out, key=lambda x: x[1])[1] + 1
    width = max(out, key=lambda x: x[0])[0] + 1
    tiles = [[TileSet.EMPTY] * width for __ in range(height)]
    for x, y, id in out:
        tiles[y][x] = TileSet(id)
    return out, tiles, height, width
Example #6
0
def paint_hull(memory: list) -> Union[dict, dict]:
    program = IntcodeComputer(memory)
    hull = defaultdict(lambda: PANELS.BLACK)
    painted = defaultdict(lambda: 0)

    res = IntcodeComputer.OUTPUT_CODES.OK
    loc = (0, 0)
    facing = DIRECTION.NORTH
    while res != IntcodeComputer.OUTPUT_CODES.HALTED:
        panel_color = hull[loc]
        out = []
        res = program.execute([COLOR_CODES[panel_color]], out)
        hull[loc] = COLOR_CODES[out[0]]
        painted[loc] += 1
        loc, facing = move(loc, facing, out[1])
    return hull, painted
Example #7
0
def run_game(memory:list) -> list:
    program = IntcodeComputer(memory)
    out = []
    program.execute(deque(), out)
    return [out[i:i + 3] for i in range(0, len(out), 3)]
Example #8
0
def run_ascii_program(memory: list) -> list:
    program = IntcodeComputer(memory)
    out = []
    program.execute([], out)
    return list(map(chr, out))
Example #9
0
def BOOST_program(memory: list) -> int:
    program = IntcodeComputer(memory)
    out = []
    res = program.execute(deque([2]), out)
    return out.pop()
Example #10
0
def in_beam(program:IntcodeComputer, x:int, y:int) -> int:
    out = []
    program.execute(deque([x,y]), out)
    program.reset()
    return out.pop()