Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
with collections.Counter solution in Clear category for Wild Dogs by Phil15
from collections import Counter
from itertools import combinations
from math import hypot
def eq_and_distance(A, B):
"""Return equation of the line going through A and B
and distance from it to (0, 0)."""
xA, yA, xB, yB = *A, *B
if xA==xB: # equation x = xA
# best spot: (xA, 0) so distance == abs(xA)
return f"x = {xA}", abs(xA)
else: # equation y = m * x + p
m = (yB - yA) / (xB - xA)
p = yA - m * xA
# best spot: ((-m*p)/(1+m**2), p/(1+m**2)) ...
return f"y={m}*x+{p}", abs(p) / hypot(1, m) # distance expr simplified
def wild_dogs(coords):
"""Return distance from me to the nearest spot where
I can do the BEST multi-kill of evil dogs."""
count_eq_with_distances = Counter(eq_and_distance(*dog_pair)
for dog_pair in combinations(coords, 2))
res = min((-count, dist) # Maximize count then minimize dist. Keep min dist.
for (eq, dist), count in count_eq_with_distances.items())[1]
return int(res) if int(res)==res else round(res, 2)
Sept. 10, 2018
Comments: