def get_wall_sequence(self, origin: Point): """Succession of points against the wall starting from a given point""" sequence = [origin] if all([self.is_wall(p) for p in origin.adjacent_points()]): # the origin point is surrounded by walls return sequence loop_done = False curr = origin # get the direction where the wall is wall_dir = LEFT if self.is_wall(curr.adj(LEFT)) \ else UP if self.is_wall(curr.adj(UP)) \ else RIGHT if self.is_wall(curr.adj(RIGHT)) \ else DOWN if self.is_wall(curr.adj(DOWN)) \ else LEFT if self.is_wall(curr.adj(UP).adj(LEFT)) \ else UP if self.is_wall(curr.adj(UP).adj(RIGHT)) \ else RIGHT if self.is_wall(curr.adj(DOWN).adj(RIGHT)) \ else DOWN while not loop_done: # get the next point against the wall p = curr.adj(Point.next_dir(wall_dir)) while self.is_wall(p): # changes the run direction if a wall is found wall_dir = Point.next_dir(wall_dir) p = curr.adj(Point.next_dir(wall_dir)) sequence.append(p) loop_done = (p == sequence[0]) curr = p if not self.is_wall(curr.adj(wall_dir)): wall_dir = Point.prev_dir(wall_dir) return sequence
def possible_dirs(self, point: Point, pipe_id: int) -> list: return [ adj for adj in point.adjacent_points() if self.universe[adj.x, adj.y] == '.' or adj == self.pipe_ends[pipe_id][1] ]