-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPE11.py
91 lines (73 loc) · 2.19 KB
/
PE11.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
class LenError(Exception):
pass
def loadFile(filename):
"""Loads a file that contains numbers in a 20x20 grid.
Loads those numbers as ints into a 20x20 array.
Returns that array."""
finalArray = []
with open(filename) as f:
for line in f:
splitLine = line.split()
temp = []
for j in splitLine:
temp.append(int(j))
finalArray.append(temp)
return finalArray
def horizontal(x, y, array):
"""Array is a 20x20 array. x is the starting location.
Find the multiplication of 4 consecutive numbers
on the same line.
We do the check in here to make sure that there are
four numbers to add up."""
if len(array[y]) - x < 4:
raise LenError("There are not enough ints in that direction")
val = 1
for i in range(4):
val *= array[y][x+i]
print val
return val
def vertical(x, y, array):
if y > 15:
raise LenError("There are not enough ints in that direction")
val = 1
for i in range(4):
val *= array[y+i][x]
print val
return val
def lDiag(x, y, array):
"""Diagonals up towards left."""
if len(array[y]) - x < 4 or y < 4:
raise LenError("There are not enough ints in that direction")
val = 1
for i in range(4):
val *= array[y-i][x-i]
print val
return val
def rDiag(x, y, array):
"""Diagonals up and to the right"""
if len(array[y]) - x < 4 or y < 4:
raise LenError("There are not enough ints in that direction")
val = 1
for i in range(4):
val *= array[y-i][x+i]
print val
return val
myArray = loadFile("PE11.hlp")
myTot = 0
for i in range(20):
for j in range(20):
for k in range(4):
try:
if k == 0:
temp = horizontal(i, j, myArray)
elif k == 1:
temp = vertical(i, j, myArray)
elif k == 2:
temp = lDiag(i, j, myArray)
else:
temp = rDiag(i, j, myArray)
if temp > myTot:
myTot = temp
except LenError:
pass
print myTot