Exemplo n.º 1
0
def explore(program: Program) -> Grid:
    program.reset()
    available_moves: DefaultDict[complex,
                                 List[int]] = defaultdict(lambda: [4, 2, 3, 1])
    backtracking: List[int] = list()
    grid: Grid = dict()
    position = 0j
    available_moves[position]  # init to enter the loop
    while any(available_moves.values()) or backtracking:
        if available_moves[position]:
            direction = available_moves[position].pop()
            program.add_input(direction)
            status = next(program.operate())
            grid[position + moves[direction]] = status
            if status in [1, 2]:
                backtracking.append(back[direction])
                position += moves[direction]
            else:
                # We hit a wall, don't move
                continue
        else:
            # no move available, backtrack
            direction = backtracking.pop()
            program.add_input(direction)
            next(program.operate())  # we know it will be 1
            position += moves[direction]
    return grid
Exemplo n.º 2
0
def get_ascii(program: Program) -> str:
    grid = []
    while True:
        try:
            grid.append(chr(next(program.operate())))
        except StopIteration:
            break
    return "".join(grid)
Exemplo n.º 3
0
def test_corners(program: Program, x: int, y: int) -> bool:
    for xx, yy in [(x, y), (x + 99, y), (x, y + 99), (x + 99, y + 99)]:
        program.reset()
        program.add_input(xx)
        program.add_input(yy)
        if next(program.operate()) != 1:
            return False
    return True
Exemplo n.º 4
0
def scan(program: Program, shape: Tuple[int, int] = (50, 50)) -> np.array:
    grid = np.zeros(shape, dtype=int)
    for x in range(shape[0]):
        for y in range(shape[1]):
            program.add_input(x)
            program.add_input(y)
            grid[x][y] = next(program.operate())
            program.reset()
    return grid