project-euler/102_triangle_containment.py

24 lines
695 B
Python

#!/usr/bin/env python3
# Calculate area of triangle and compare to areas
# of triangles with one vertex = origin
# better solution -> check sign of cross products
def main():
tr = parse("data/102_triangles.txt")
return len([0 for p in tr if areaTR(p) == areaOG(0,2,p) + areaOG(0,4,p) + areaOG(2,4,p)])
def areaTR(p): #triangle area
return abs(p[0]*p[3] - p[0]*p[5] +
p[2]*p[5] - p[2]*p[1] +
p[4]*p[1] - p[4]*p[3])
def areaOG(a, b, p): #triangle with with a=0,0
return abs(p[a]*p[b+1] - p[b]*p[a+1])
def parse(path):
with open(path) as f:
return [list(map(int, row.split(","))) for row in f.read()[:-1].split("\n")]
print(main())