Ejemplo n.º 1
0
def paint(start_color):
    p = (0, 0)
    d = UP
    grid = {}
    ic = Intcode(input_ints)
    while True:
        color = grid.get(p, 0) if grid else start_color
        ic.add_input(color)
        outputs = list(ic.run_safe())
        if not outputs:
            break
        color, turn = outputs
        grid[p] = color
        d = turn_dir(d, turn)
        p = move_point(p, d)
    return grid
Ejemplo n.º 2
0
class Robot:
    def __init__(self):
        self.ic = Intcode(input_ints)
        self.grid = {}
        self.pos = (0, 0)
        self.moves = []

    def next_pos(self, move):
        dx, dy = VECTORS[move]
        return self.pos[0] + dx, +self.pos[1] + dy

    def move(self, move):
        next_pos = self.next_pos(move)
        self.ic.add_input(move)
        output = list(self.ic.run_safe())[0]
        if output not in VALID_OUTPUT:
            raise Exception(f"invalid output {output}")
        self.grid[next_pos] = output
        if output == WALL:
            return None
        self.pos = next_pos
        return self.pos

    def new_move(self):
        for move in [NORTH, SOUTH, EAST, WEST]:
            next_pos = self.next_pos(move)
            if next_pos in self.grid:
                continue
            pos = self.move(move)
            if pos:
                return move
        return None

    def map_grid(self):
        move = self.new_move()
        if not move:
            raise Exception("first move failed")
        self.moves.append(move)
        while self.moves:
            move = self.new_move()
            if move:
                self.moves.append(move)
            else:
                self.move(INVERSE[self.moves.pop()])