Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
flood fill solution in Clear category for Safe Coasts by gyahun_dash
from itertools import starmap, chain, product
fourway = lambda r, c: ((r, c), (r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1))
eightway = lambda r, c: product(range(r - 1, r + 2), range(c - 1, c + 2))
def finish_map(region, D = 'D', X = 'X', S = 'S'):
rows, cols = map(range, map(len, (region, region[0])))
cells = set(product(rows, cols))
ingroup = lambda mark: lambda cell: region[cell[0]][cell[1]] == mark
land, danger = [set(filter(ingroup(g), cells)) for g in (X, D)]
def erode(around: fourway or eightway, area: 'set of tuples') -> 'set':
return set(chain(*starmap(around, area))) & cells
safe = erode(eightway, land)
while danger < erode(fourway, danger) - safe:
danger = erode(fourway, danger) - safe
det = lambda *cell: D if (cell in danger) else X if (cell in land) else S
return [''.join(det(r, c) for c in cols) for r in rows]
Aug. 30, 2014
Comments: