-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectEuler81.py
78 lines (65 loc) · 2.01 KB
/
ProjectEuler81.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# coding:utf-8
"""
Project Euler Problem 81
"""
import time
time1 = time.time()
rows = []
f = open("projecteuler81matrix.txt", "r")
for line in f:
rows.append(list(map(int, line[:-1].split(","))))
f.close()
# problem 67 no youni triangle wo tsukutte toku. ue hanbun to shita hanbun.
sup_tri = []
sub_tri = []
for i in range(0, 80):
ins = []
for j in range(0, i + 1):
ins.append(rows[i - j][j])
sup_tri.append(ins)
for i in range(0, 79):
ins = []
for j in range(0, i + 1):
ins.append(rows[79 - j][79 - i + j])
sub_tri.append(ins)
sub_tri_r = sub_tri[::-1]
sup_tri_r = sup_tri[::-1]
for i in range(len(sup_tri) - 1):
sup_tri[i + 1][0] += sup_tri[i][0]
sup_tri[i + 1][-1] += sup_tri[i][-1]
for j in range(len(sup_tri[i]) - 1):
if sup_tri[i][j] < sup_tri[i][j + 1]:
sup_tri[i + 1][j + 1] += sup_tri[i][j]
else:
sup_tri[i + 1][j + 1] += sup_tri[i][j + 1]
for i in range(len(sub_tri) - 1):
sub_tri[i + 1][0] += sub_tri[i][0]
sub_tri[i + 1][-1] += sub_tri[i][-1]
for j in range(len(sub_tri[i]) - 1):
if sub_tri[i][j] < sub_tri[i][j + 1]:
sub_tri[i + 1][j + 1] += sub_tri[i][j]
else:
sub_tri[i + 1][j + 1] += sub_tri[i][j + 1]
"""
for i in xrange(len(sup_tri_r)-1):
for j in xrange(0,len(sup_tri_r)-i-1):
if sup_tri_r[i][j]<=sup_tri_r[i][j+1]:
sup_tri_r[i+1][j]+=sup_tri_r[i][j]
else:
sup_tri_r[i+1][j]+=sup_tri_r[i][j+1]
for i in xrange(len(sub_tri_r)):
for j in xrange(0,len(sub_tri_r)-i-1):
if sub_tri_r[i][j]<=sub_tri_r[i][j+1]:
sub_tri_r[i+1][j]+=sub_tri_r[i][j]
else:
sub_tri_r[i+1][j]+=sub_tri_r[i][j+1]
"""
answers_ = []
for i in range(len(sup_tri[-1]) - 1):
if sup_tri[-1][i] < sup_tri[-1][i + 1]:
answers_.append(sub_tri[-1][i] + sup_tri[-1][i])
else:
answers_.append(sub_tri[-1][i] + sup_tri[-1][i + 1])
print(min(answers_))
print(time.time() - time1, "seconds")