Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Black Holes by flpo
from itertools import starmap, product
from math import acos, sqrt, pi, sin
class BlackHole:
def __init__(self, x, y, r):
self.x = x
self.y = y
self.set_radius(r)
def set_radius(self, r):
self.r = r
self.r2 = r ** 2
self.area = pi * r**2
def dist(self, other):
return sqrt((self.x - other.x) ** 2 + (self.y - other.y) ** 2)
def absorbs(self, other):
self.set_radius(sqrt(self.r2 + other.r2))
def can_absorbs(self, other):
dist = self.dist(other)
if dist > self.r + other.r:
return False
if self.area / other.area < 1.2:
return False
if dist <= abs(self.r - other.r) and self.r >= other.r:
return True
phi = acos((self.r2 + dist**2 - other.r2) / (2 * self.r * dist)) * 2
theta = acos((other.r2 + dist**2 - self.r2) / (2 * self.r * dist)) * 2;
area1 = 0.5 * theta * other.r2 - 0.5 * other.r2 * sin(theta)
area2 = 0.5 * phi * self.r2 - 0.5 * self.r2 * sin(phi)
return (area1 + area2) / other.area > .55
def as_tuple(self):
return (self.x, self.y, self.r)
def checkio(holes_list):
holes = list(starmap(BlackHole, holes_list))
ordered = sorted(holes, key=lambda hole: sum(map(hole.dist, holes)))
while True:
try:
h, a = next((h, a) for h, a in product(ordered, holes) if h.can_absorbs(a))
h.absorbs(a)
holes.remove(a)
except StopIteration:
return [hole.as_tuple() for hole in holes]
July 18, 2017