Exemple #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]
Exemple #2
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
Exemple #3
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]
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
Exemple #5
0
def monitor_network(n_computers: int, memory: list) -> Union[int, int]:
    computers = [IntcodeComputer(memory.copy()) for __ in range(n_computers)]
    in_queue = [deque([i]) for i in range(n_computers)]
    out_queue = [deque() for __ in range(n_computers)]
    NAT = []
    NAT_monitor = Counter()
    while True:
        # computers
        for i in range(n_computers):
            in_queue[i] = deque([-1]) if not in_queue[i] else in_queue[i]
            computers[i].execute(in_queue[i], out_queue[i])

        # router
        for i in range(n_computers):
            while out_queue[i]:
                addr = out_queue[i].popleft()
                x = out_queue[i].popleft()
                y = out_queue[i].popleft()
                if addr == 255:
                    NAT = [x, y]
                else:
                    in_queue[addr] += deque([x, y])

        # NAT
        if NAT and not any(in_queue):
            x, y = NAT
            in_queue[0] += deque([x, y])
            NAT_monitor[y] += 1
            if NAT_monitor[y] == 2:
                return y
Exemple #6
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
Exemple #7
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
Exemple #8
0
def boot_network(n_computers: int, memory: list) -> Union[int, int]:
    computers = [IntcodeComputer(memory.copy()) for __ in range(n_computers)]
    in_queue = [deque([i]) for i in range(n_computers)]
    out_queue = [deque() for __ in range(n_computers)]
    while True:
        # computers
        for i in range(n_computers):
            in_queue[i] = deque([-1]) if not in_queue[i] else in_queue[i]
            computers[i].execute(in_queue[i], out_queue[i])

        # router
        for i in range(n_computers):
            while out_queue[i]:
                addr = out_queue[i].popleft()
                x = out_queue[i].popleft()
                y = out_queue[i].popleft()
                if addr == 255:
                    return x, y
                in_queue[addr] += deque([x, y])
Exemple #9
0
def scan_area(memory:list, x_fit, y_fit, start_pos=None) -> Union[list, int]:
    program = IntcodeComputer(memory)
    affected = 0
    positions = [start_pos] if start_pos else [(0,0)]
    visited = set()
    while positions:
        rx, ry = positions.pop()
        if (rx, ry) in visited:
            continue
        visited.add((rx, ry))
        in_beam(program, rx, ry)

        if in_beam(program, rx, ry):    
            lx = rx - (x_fit-1)
            ly = ry + (y_fit-1)
            if lx >= 0 and ly >= 0:
                if in_beam(program, lx, ly):
                    break
            positions += [(rx, ry+1), (rx+1, ry)]
    return (lx, ry)
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)]
Exemple #11
0
def run_ascii_program(memory: list) -> list:
    program = IntcodeComputer(memory)
    out = []
    program.execute([], out)
    return list(map(chr, out))
def BOOST_program(memory: list) -> int:
    program = IntcodeComputer(memory)
    out = []
    res = program.execute(deque([2]), out)
    return out.pop()
Exemple #13
0
def in_beam(program:IntcodeComputer, x:int, y:int) -> int:
    out = []
    program.execute(deque([x,y]), out)
    program.reset()
    return out.pop()