Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
circle to chains solution in Clear category for Count Chains by Alexey.K.
from typing import List, Tuple
from math import dist
def is_intersect(circle1: Tuple[int, int, int], circle2: Tuple[int, int, int]):
print("intersect:",circle1,circle2)
d = dist(circle1[0:2],circle2[0:2])
r0,r1 = circle1[2],circle2[2]
return 1 if dabs(r0-r1) else 0
def add_c_to_c(circle: Tuple[int, int, int], chains: List) -> List:
candidate=[]
print("try add", circle, "to chains", chains)
newchain=[circle]
oldchains=[]
if chains:
for chain in chains:
print("check chain:",chain)
if sum(is_intersect(circle, item) for item in chain) > 0:
newchain+=chain
else:
oldchains+=[chain]
oldchains+=[newchain]
return oldchains
def count_chains(circles: List[Tuple[int, int, int]]) -> int:
chains=[]
for circle in circles:
chains=add_c_to_c(circle,chains)
return len(chains)
if __name__ == '__main__':
print("Example:")
print(count_chains([[1,3,1],[2,2,1],[4,2,1],[5,3,1],[3,3,1]]))
# These "asserts" are used for self-checking and not for an auto-testing
assert count_chains([(1, 1, 1), (4, 2, 1), (4, 3, 1)]) == 2, 'basic'
assert count_chains([(1, 1, 1), (2, 2, 1), (3, 3, 1)]) == 1, 'basic #2'
assert count_chains([(2, 2, 2), (4, 2, 2), (3, 4, 2)]) == 1, 'trinity'
assert count_chains([(2, 2, 1), (2, 2, 2)]) == 2, 'inclusion'
assert count_chains([(1, 1, 1), (1, 3, 1), (3, 1, 1), (3, 3, 1)]) == 4, 'adjacent'
assert count_chains([(0, 0, 1), (-1, 1, 1), (1, -1, 1), (-2, -2, 1)]) == 2, 'negative coordinates'
print("Coding complete? Click 'Check' to earn cool rewards!")
March 16, 2021