-
Notifications
You must be signed in to change notification settings - Fork 2
/
p011.py
60 lines (46 loc) · 1.56 KB
/
p011.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
#!/usr/bin/env python
# Project Euler
# Problem 11
# http://projecteuler.net/problem=11
# What is the greatest product of four adjacent numbers in any direction
# (up, down, left, right, or diagonally) in the 20x20 grid (stored in file
# 'problem11_data.txt')?
if __name__ == "__main__":
data = open('problem11_data.txt','r')
grid = data.readlines()
for i in xrange(len(grid)):
grid[i] = grid[i].split(' ')
for j in xrange(len(grid[i])):
grid[i][j] = int(grid[i][j]) # cast all from string to int
# brute force the solution
largest = 0
# do left/right
for i in xrange(len(grid)):
for j in xrange(len(grid[i])-3):
multiplied = grid[i][j]*grid[i][j+1]*grid[i][j+2]*grid[i][j+3]
if( multiplied > largest ):
largest = multiplied
for i in xrange(len(grid)-3):
# do up/down
for j in xrange(len(grid[i])):
multiplied = grid[i][j]*grid[i+1][j]*grid[i+2][j]*grid[i+3][j]
if( multiplied > largest ):
largest = multiplied
# do diagnally (top to bottom)
for j in xrange(len(grid[i])-3):
multiplied = grid[i][j]*grid[i+1][j+1]*grid[i+2][j+2]*grid[i+3][j+3]
if( multiplied > largest ):
largest = multiplied
# do diagnally (bottom to top)
for j in xrange(len(grid[i])-3):
multiplied = grid[i][j+3]*grid[i+1][j+2]*grid[i+2][j+1]*grid[i+3][j]
if( multiplied > largest ):
largest = multiplied
print largest
#i=6
#j=8
#multiplied = grid[i][j]*grid[i+1][j+1]*grid[i+2][j+2]*grid[i+3][j+3]
#if( multiplied > largest ):
#largest = multiplied
#print largest
#print grid[i][j],grid[i+1][j],grid[i+2][j],grid[i+3][j]