Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Triangular Islands by eugene100372
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
if __name__ == '__main__':
print("Example:")
print(sorted(triangular_islands({1})))
# These "asserts" are used for self-checking and not for an auto-testing
assert sorted(triangular_islands({1})) == [1]
assert sorted(triangular_islands({2, 3, 6})) == [3]
assert sorted(triangular_islands({4, 3})) == [2]
assert sorted(triangular_islands({1, 4, 7, 8})) == [1, 3]
assert sorted(triangular_islands({1, 2, 3, 4, 5, 6, 7, 8, 9})) == [9]
assert sorted(triangular_islands({1, 2, 4, 5, 7, 9})) == [1, 1, 1, 1, 1, 1]
print("Coding complete? Click 'Check' to earn cool rewards!")
July 31, 2020