Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Bacteria Colonies solution in Uncategorized category for Bacteria Colonies by capback250
# migrated from python 2.7
COORDS = ((-1, 0), (0, -1), (0, 1), (1, 0))
def healthy(grid):
finder = [checker(grid, i1, i2) for i1, v1 in enumerate(grid) for i2, v2 in enumerate(v1) if v2]
return sorted(filter(bool, finder), key=lambda x: x[1])[-1][0] if list(filter(bool, finder)) else (0, 0)
def checker(grid, x, y):
visited, depth, stack = set(), 0, [[x, y]]
visited.add((x, y))
while 1:
around, cors = [], []
for point in stack:
for c in COORDS:
dx, dy = c[0] + point[0], c[1] + point[1]
if dx >= 0 and dy >= 0 and dx < len(grid) and dy < len(grid[0]):
if (dx, dy) not in visited:
around.append(grid[dx][dy])
visited.add((dx, dy))
cors.append([dx, dy])
depth += 1
if sum(around) < 4 and depth == 1:
return False
if len(set(around)) > 1:
return False
if sum(around) == len(cors):
stack = cors
if all([x == 0 for x in around]) and depth:
return ((x, y), depth)
Feb. 9, 2016