Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
I'll just return a map. solution in Clear category for Hexagonal Islands by veky
import itertools, collections
def neighbors(hex):
i, j = hex
for t in -1, 1:
yield i + t, j
for s in 0, (-1)**j: yield i + s, j + t
def components(hexes):
while hexes:
component = {hexes.pop()}
q = collections.deque(component)
while q:
for near in neighbors(q.popleft()):
if near in hexes:
for f in hexes.remove, component.add, q.append: f(near)
if (0, 0) not in component: yield component
def hexagonal_islands(coast_marks):
coast_hexes = {(int(row), int(col, 36) - 9) for col, row in coast_marks}
inner_hexes = {*itertools.product(range(11), range(14))} - coast_hexes
coasts = list(components(coast_hexes))
for land in components(inner_hexes):
land.update(*map(neighbors, land))
for coast in coasts:
if coast & land: coast |= land
return map(len, coasts)
July 11, 2019
Comments: