-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler011.py
executable file
·73 lines (58 loc) · 2.06 KB
/
euler011.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
#!/usr/bin/python
import scipy
import sys
if sys.version_info[0] == 2:
# get rid of 2.x range that produced list instead of iterator
range = xrange
def getGrid(fileName):
grid = []
with open(fileName, 'r') as fIn:
for line in fIn:
grid.append([int(n) for n in line.split()])
return scipy.array(grid)
def genNegSlopeDiagonal(grid, numElements):
numRows, numCols = grid.shape
nEM1 = numElements - 1
for row in range(numRows - nEM1):
for col in range(numCols - nEM1):
diag = grid[range(row, row + numElements),
range(col, col + numElements)]
yield diag
def genPosSlopeDiagonal(grid, numElements):
numRows, numCols = grid.shape
nEM1 = numElements - 1
for row in range(nEM1, numRows):
for col in range(numCols - nEM1):
diag = grid[range(row, row - numElements, -1),
range(col, col + numElements)]
yield diag
def genHoriz(grid, numElements):
numRows, numCols = grid.shape
nEM1 = numElements - 1
for row in range(numRows):
for col in range(0, numCols - nEM1):
horiz = grid[row, range(col, col + numElements)]
yield horiz
def genVert(grid, numElements):
numRows, numCols = grid.shape
nEM1 = numElements - 1
for row in range(numRows - nEM1):
for col in range(numCols):
vert = grid[range(row, row + numElements), col]
yield vert
def genLine(grid, numElements):
for seg in genNegSlopeDiagonal(grid, numElements):
yield (scipy.prod(seg), seg, 'negative slope')
for seg in genPosSlopeDiagonal(grid, numElements):
yield (scipy.prod(seg), seg, 'positive slope')
for seg in genHoriz(grid, numElements):
yield (scipy.prod(seg), seg, 'horizontal')
for seg in genVert(grid, numElements):
yield (scipy.prod(seg), seg, 'vertical')
def euler11(gridFile='data/euler011.txt', numElements=4):
grid = getGrid(gridFile)
maxSeg = max(genLine(grid, numElements), key=lambda x: x[0])
print('Greatest product is %d, From %s line with elements: %s'
% (maxSeg[0], maxSeg[2], str(maxSeg[1])))
if __name__ == "__main__":
euler11()