Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Complex numbers and frames of reference solution in Clear category for Wild Dogs by Leonix
import itertools
def wild_dogs(coords):
best_candidate = max(candidates(coords))
return round(best_candidate[2], 2)
def candidates(coords):
"""
So, the plan is. Convert all coordinates to complex numbers.
For each pair of dogs, convert all coordinates to frame of reference in which
first dog of the pair is at 0+0j and second is at 1+0j.
In new frame of reference, all dogs with zero imag coordinate
are on the line that goes through chosen pair of dogs.
"""
coords = [x + 1j*y for x, y in coords]
for d1, d2 in itertools.combinations(coords, 2):
count_in_line = sum(1 for d in coords if convert(d1, d2, d).imag == 0)
best_spot = convert_back(d1, d2, convert(d1, d2, 0).real)
yield(count_in_line, -abs(best_spot), abs(best_spot))
def convert(d1, d2, d):
return (d - d1) / (d2 - d1)
def convert_back(d1, d2, d):
return d * (d2 - d1) + d1
April 14, 2019