Solve problem 102

main
Felix Martin 2021-06-09 16:16:15 -04:00
parent c51fdc707f
commit 5f32a5f18a
2 changed files with 1051 additions and 0 deletions

51
python/e102.py Normal file
View File

@ -0,0 +1,51 @@
from math import acos, sqrt, pi, atan
import matplotlib.pyplot as plt
def triangle(euler_text_line):
ps = list(map(int, euler_text_line.split(",")))
ps = [(ps[0], ps[1]), (ps[2], ps[3]), (ps[4], ps[5])]
return ps
def angle_x(x, y):
if x > 0 and y == 0:
return 0
elif x < 0 and y == 0:
return pi
elif x == 0 and y > 0:
return pi / 2
elif x == 0 and y < 0:
return 3 / 2 * pi
elif x > 0 and y > 0:
return atan(y / x)
elif x < 0 and y > 0:
return pi - atan(y / abs(x))
elif x < 0 and y < 0:
return pi + atan(abs(y) / abs(x))
elif x > 0 and y < 0:
return 2 * pi - atan(abs(y) / x)
else:
raise Exception("Unhandled case")
def contains_origin(triangle):
a, b, c = triangle
angles = sorted([angle_x(*a), angle_x(*b), angle_x(*c)])
a = angles[1] - angles[0]
b = angles[2] - angles[1]
c = angles[0] + 2 * pi - angles[2]
return not (a + b < pi or b + c < pi or c + a < pi)
def euler_102():
with open("../txt/e102.txt", "r") as f:
triangles = list(map(triangle, f.readlines()))
return len([1 for t in triangles if contains_origin(t)])
if __name__ == "__main__":
solution = euler_102()
print("e102.py: " + str(solution))
assert(solution == 228)

1000
txt/e102.txt Normal file

File diff suppressed because it is too large Load Diff