Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Black Holes by lok7chan
import math
import itertools
def holes_distance(pairs):
return math.sqrt((pairs[0][0]-pairs[1][0])**2+(pairs[0][1]-pairs[1][1])**2)
def checkio(data):
updated = True
while updated:
updated = False
compare_list = list(itertools.combinations(data,2))
compare_list = sorted(compare_list, key=holes_distance)
for pairs in compare_list:
r1, r2 = pairs[0][2], pairs[1][2]
s1, s2 = math.pi*r1**2, math.pi*r2**2
d = holes_distance(pairs)
if r1+r2 > d:
R, r = sorted([r1,r2],reverse=True)
if d + r < R:
s_intersect = min(s1,s2)
else:
s_intersect = (r**2)*math.acos((d**2+r**2-R**2)/(2*d*r))+ (R**2)*math.acos((d**2+R**2-r**2)/(2*d*R)) -(1/2)*math.sqrt((-d+r+R)*(d+r-R)*(d-r+R)*(d+r+R))
if s_intersect >= s1*0.55 or s_intersect >= s2*0.55:
if (s1-s2)/s2 >= 0.2 or (s2-s1)/s1 >= 0.2:
new_hole, remove_hole = sorted(pairs, key=lambda p: p[2], reverse=True)
data.remove(remove_hole)
for i, hole in enumerate(data):
if hole == new_hole:
new_hole = (new_hole[0], new_hole[1], math.sqrt((s1+s2)/math.pi))
data[i] = new_hole
updated = True
break
return data
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio([(2, 4, 2), (3, 9, 3)]) == [(2, 4, 2), (3, 9, 3)]
assert checkio([(0, 0, 2), (-1, 0, 2)]) == [(0, 0, 2), (-1, 0, 2)]
assert checkio([(4, 3, 2), (2.5, 3.5, 1.4)]) == [(4, 3, 2.44)]
assert checkio([(3, 3, 3), (2, 2, 1), (3, 5, 1.5)]) == [(3, 3, 3.5)]
Jan. 28, 2016
Comments: