Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Rows of Cakes by Moff
from itertools import combinations
from collections import namedtuple
Point = namedtuple('Point', 'x y')
class Line(namedtuple('Line', 'a b c')):
@classmethod
def from_points(cls, p1, p2):
a, b, c = (p2.y-p1.y, p1.x - p2.x, p1.y * p2.x - p1.x * p2.y)
d = gcd(gcd(a, b), c)
return cls(a // d, b // d, c // d)
def gcd(a, b):
while b:
a, b = b, a % b
return a
def is_collinear(a, b, c):
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x) == 0
def checkio(points):
points = [Point(*p) for p in points]
lines = set()
for c in combinations(points, 3):
if is_collinear(*c):
lines.add(Line.from_points(c[0], c[1]))
return len(lines)
Aug. 13, 2015
Comments: