from collections import Counter class Grid(): dim = 12 def __init__(self): self.cells = [(x, y) for x in range(Grid.dim) for y in range(Grid.dim)] def neighbors(self, x, y, diags=False): neighs = [(x, y - 1), (x - 1, y), (x + 1, y), (x, y + 1)] if diags: neighs += [(x - 1, y - 1), (x + 1, y - 1), (x - 1, y + 1), (x + 1, y + 1)] return [(x, y) for x, y in neighs if 0 <= x < Grid.dim and 0 <= y < Grid.dim] def _active_owned(self, pos): return pos in [(9, 9), (8, 9), (10, 9), (10, 10), (7, 9), (9, 11), (11, 10), (10, 11), (11, 9), (11, 11), (8, 11), (7, 11), (6, 11), (5, 11)] def update_pivot_for(self): start = (11,11) buffer = [start] visited_from = {start: []} while buffer: new_buffer = [] for p in buffer: for n in self.neighbors(*p): if self._active_owned(n): if not n in visited_from: new_buffer.append(n) if not n in visited_from: visited_from[n] = [p] else: visited_from[n].append(p) buffer = new_buffer return visited_from grid = Grid() c = grid.update_pivot_for() print("\n".join([f"{k}: {v}" for k, v in c.items()]))