-
Notifications
You must be signed in to change notification settings - Fork 0
/
011.py
58 lines (43 loc) · 1.72 KB
/
011.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
import time
def check_vertical(matrix):
max_product = 0
for row in xrange(0, len(matrix)-3):
for col in xrange(0, len(matrix)):
product = matrix[row][col] * matrix[row+1][col] * matrix[row+2][col] * matrix[row+3][col]
max_product = max(product, max_product)
return max_product
def check_horizontal(matrix):
max_product = 0
for row in xrange(0, len(matrix)):
for col in xrange(0, len(matrix)-3):
product = reduce(lambda x,y: x*y, matrix[row][col:col+3])
max_product = max(product, max_product)
return max_product
def check_left_diagonal(matrix):
max_product = 0
for row in xrange(0, len(matrix)-3):
for col in xrange(0, len(matrix)-3):
product = matrix[row][col] * matrix[row+1][col+1] * matrix[row+2][col+2] * matrix[row+3][col+3]
max_product = max(product, max_product)
return max_product
def check_right_diagonal(matrix):
max_product = 0
for row in xrange(0, len(matrix)-3):
for col in xrange(0, len(matrix)-3):
product = matrix[row+3][col] * matrix[row+2][col+1] * matrix[row+1][col+2] * matrix[row][col+3]
max_product = max(product, max_product)
return max_product
def main():
with open("011.txt", "r") as f:
# Read the matrix from the text file, and store in an integet 2-dimensional array
matrix = []
for line in f.readlines():
matrix.append([int(num) for num in line.split(" ")])
# print matrix
# Check the matrix along the various directions, and find the max product of four adjacent numbers
print("The result is %d." % max(check_vertical(matrix), check_horizontal(matrix), check_left_diagonal(matrix), check_right_diagonal(matrix)))
if __name__ == '__main__':
start = time.time()
main()
done = time.time()
print("The solution took %.4f seconds to compute." % (done - start))