-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.py
50 lines (35 loc) · 1.16 KB
/
day3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# vi: set shiftwidth=4 tabstop=4 expandtab:
import datetime
import os
top_dir = os.path.dirname(os.path.abspath(__file__)) + "/../../"
def get_triangle_from_line(line):
return tuple(int(v) for v in line.split())
def get_triangles_from_file(file_path=top_dir + "resources/year2016_day3_input.txt"):
with open(file_path) as f:
return [get_triangle_from_line(l.strip()) for l in f]
def triangle_is_possible(t):
a, b, c = sorted(t)
return a + b > c
def run_tests():
assert not triangle_is_possible((5, 10, 25))
assert triangle_is_possible((5, 10, 12))
def get_solutions():
triangles = get_triangles_from_file()
print(sum(triangle_is_possible(t) for t in triangles) == 993)
s = 0
triangles_iter = iter(triangles)
while True:
try:
for t in zip(
next(triangles_iter), next(triangles_iter), next(triangles_iter)
):
s += triangle_is_possible(t)
except StopIteration:
break
print(s == 1849)
if __name__ == "__main__":
begin = datetime.datetime.now()
run_tests()
get_solutions()
end = datetime.datetime.now()
print(end - begin)