-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEuler problem #11.py
148 lines (103 loc) · 4.2 KB
/
Euler problem #11.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# RPI2020
# Idea, build functions to calculate the product in each of the 8 directions, I've done up and left
#call up on Euler[10][11], up(Euler, 10, 11)
#call left on Euler[10][11], left(Euler[10], 11)
import time
def up(array, column_index, row_index):
return array[row_index][column_index] * array[row_index - 1][column_index] * array[row_index - 2][column_index] * \
array[row_index - 3][column_index]
def down(array, column_index, row_index):
return array[row_index][column_index] * array[row_index + 1][column_index] * array[row_index + 2][column_index] * \
array[row_index + 3][column_index]
def left(row_array, column_index):
return row_array[column_index] * row_array[column_index - 1] * row_array[column_index - 2] * row_array[
column_index - 3]
def right(row_array, column_index):
return row_array[column_index] * row_array[column_index + 1] * row_array[column_index + 2] * row_array[
column_index + 3]
def up_left(array, column_index, row_index):
return array[row_index][column_index] * array[row_index - 1][column_index - 1] * array[row_index - 2][
column_index - 2] * \
array[row_index - 3][column_index - 3]
def up_right(array, column_index, row_index):
return array[row_index][column_index] * array[row_index - 1][column_index + 1] * \
array[row_index - 2][column_index + 2] * array[row_index - 3][column_index + 3]
def down_left(array, column_index, row_index):
return array[row_index][column_index] * array[row_index - 1][column_index - 1] * \
array[row_index - 2][column_index - 2] * array[row_index - 3][column_index - 3]
def down_right(array, column_index, row_index):
return array[row_index][column_index] * array[row_index + 1][column_index - 1] * \
array[row_index + 2][column_index - 2] * array[row_index + 3][column_index - 3]
Euler = [[-1] * 20 for x in range(20)]
Euler[15][12] = 20
# print(Euler[][12])
f = open('Euler11numbers.txt', 'r')
# f.readline()[4] gets the character at index 4 in the line that was just read
# ALGORITHM
# for x in range 20 to get each line, which represents a row in the 2x2 array
for y in range(20):
line = f.readline()
# for y in range(0,20,3) counts from 0 to 20 by 3's allowing us to skip spaces
for x in range(0, 60, 3):
# Euler[x][y] = substring y to y+1 cast to an int; int()
Euler[y][int(x / 3)] = int(line[x:x + 2])
max = 0
tester = 0
start_time = time.time()
for y in range(20):
for x in range(20):
if (x >= 3):
tester = left(Euler[y], x)
if (tester > max):
max = tester
if (x <= 16):
tester = right(Euler[y], x)
if (tester > max):
max = tester
if (y >= 3):
tester = up(Euler, x, y)
if (tester > max):
max = tester
if (y <= 16):
tester = down(Euler, x, y)
if (tester > max):
max = tester
if (x >= 3 and y >= 3):
tester = up_left(Euler, x, y)
if (tester > max):
max = tester
if (y >= 3 and x <= 16):
tester = up_right(Euler, x, y)
if (tester > max):
max = tester
if (y <= 16 and x >= 3):
tester = down_left(Euler, x, y)
if (tester > max):
max = tester
if (x <= 15 and y <= 16):
tester = down_right(Euler, x, y)
if (tester > max):
max = tester
print(max, '\n', (time.time() - start_time) * 1000)
max = 0
tester = 0
start_time = time.time()
for y in range(20):
for x in range(20):
if (x <= 16):
tester = right(Euler[y], x)
if (tester > max):
max = tester
if (y <= 16):
tester = down(Euler, x, y)
if (tester > max):
max = tester
if (y >= 3 and x <= 16):
tester = up_right(Euler, x, y)
if (tester > max):
max = tester
if (x <= 15 and y <= 16):
tester = down_right(Euler, x, y)
if (tester > max):
max = tester
print(max, '\n', (time.time() - start_time) * 1000)