Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Triangular Islands by _Chico_
from typing import Set, Iterable
def neighbors(x):
n=int((x-1)**(1/2)+1)
return [x+1]*(x!=n**2)+[x-1]*(x!=(n-1)**2+1)+[x+(2*n-1)*(-1)**(x%2+n%2)+1]
def triangular_islands(triangles: Set[int]) -> Iterable[int]:
res=[]
while triangles:
island={triangles.pop()}
new=set(island)
while new:
for el in set(new):
new|=set(neighbors(el))&triangles
new-=island
island|=new
triangles-=island
res.append(len(island))
return res
May 15, 2021