-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler_p11.py
45 lines (38 loc) · 1.19 KB
/
euler_p11.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
#Euler - problem 11
"""In the 20x20 grid in numbergrid.txt, what is the greatest product of four
adjacent numbers in the same direction (up, down, left, right, or diagonally)"""
# usage: python euler_p11.py numbergrid.txt
import sys
f = open(sys.argv[1], 'rU')
digits = f.read()
numgrid = [map(int, digits.split()[i:i + 20]) for i in range(0, 381, 20)]
#up-down
maxprod = 0
for col in range(0, 20):
for row in range(0, 17):
prod = 1
for i in range(0,4):
prod = prod*numgrid[row + i][col]
maxprod = max(maxprod, prod)
#left-right
for row in range(0, 20):
for col in range(0, 17):
prod = 1
for i in range(0,4):
prod = prod*numgrid[row][col + i]
maxprod = max(maxprod, prod)
# diagonally - forward slash pattern
for row in range(0, 17):
for col in range(0, 17):
prod = 1
for i in range(0,4):
prod = prod*numgrid[row + i][col + i]
maxprod = max(maxprod, prod)
# diagonally - backslash pattern
for row in range(0, 17):
for col in range(19, 2, -1):
prod = 1
for i in range(0,4):
prod = prod*numgrid[row + i][col - i]
maxprod = max(maxprod, prod)
print maxprod