Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Count Chains solution in Speedy category for Count Chains by Tavisevik
from typing import List, Tuple
def distance(circles, i, j):
dc = ((circles[i][0]-circles[j][0])**2 + (circles[i][1]-circles[j][1])**2)**0.5
ri = (circles[i][2])
rj = (circles[j][2])
return (dc int:
n = len(circles)
color = [i+1 for i in range(n)]
gr = 1
for i in range(n):
for j in range(i+1, n):
if distance(circles, i, j):
if color[i] != color[j]:
min_color = min(color[i], color[j])
max_color = max(color[i], color[j])
for k in range(n):
if color[k] == max_color:
color[k] = min_color
return len(set(color))
if __name__ == '__main__':
print("Example:")
print(count_chains([(1, 1, 1), (4, 2, 1), (4, 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 6, 2020