Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
17-liner: Count 🔗 1 by 1 solution in Speedy category for Count Chains by Stensen
from collections import defaultdict
from math import dist
def count_chains(circles):
count, remain, chains = defaultdict(int), set(circles), 0
for circle in circles:
if not count[circle]:
chains += 1
count[circle], stack = chains, [circle]
while stack:
a, b, R = stack.pop()
for rem in list(remain):
x, y, r = rem
if abs(R - r) < dist((a, b), (x, y)) < abs(R + r):
count[rem] = chains
stack.append(rem)
remain.remove(rem)
return chains
Oct. 15, 2020