Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Vectors FTW(D) solution in Clear category for Wild Dogs by veky
import typing, itertools, math, operator
Point = typing.Tuple[int, int]
class Vector(typing.NamedTuple):
p: int
q: int
@classmethod
def arrow(cls, start, end):
"""Vector from start to end."""
return cls(*map(operator.sub, end, start))
def __invert__(self):
"""Orthogonal complement."""
return Vector(-self.q, self.p)
def __matmul__(self, other):
"""Dot product."""
return sum(map(operator.mul, self, other))
def __abs__(self):
"""Vector length."""
return math.sqrt(self @ self)
class Line(typing.NamedTuple):
point: Point
normal: Vector
@classmethod
def through(cls, A: Point, B: Point): return cls(A, ~Vector.arrow(A, B))
def __contains__(self, point: Point):
return not Vector.arrow(self.point, point) @ self.normal
def __abs__(self):
"""Distance to origin."""
return abs(Vector(*self.point) @ self.normal) / abs(self.normal)
def wild_dogs(dogs) -> typing.Union[int, float]:
d = abs(max(itertools.starmap(Line.through, itertools.combinations(dogs, 2)),
key=lambda L: (sum(dog in L for dog in dogs), -abs(L))))
return round(d, None if d.is_integer() else 2)
Sept. 12, 2018
Comments: