-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2dArray.js
36 lines (34 loc) · 926 Bytes
/
2dArray.js
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
function hourglassSum(arr) {
let sum
let max
arr.forEach((line,x) => {
line.forEach((digit,y) =>{
if (x < arr.length -2 && y < line.length -2) {
let topLeft = arr[x][y]
let topMid = arr[x][y+1]
let topRight= arr[x][y+2]
let mid = arr[x+1][y+1]
let bottomLeft = arr[x+2][y]
let bottomMid = arr[x+2][y+1]
let bottomRight = arr[x+2][y+2]
sum = topLeft + topMid + topRight + mid + bottomLeft + bottomMid + bottomRight
}
if (max == undefined) {
max = sum
}
if (sum > max) {
max = sum
}
})
})
return max
}
let inputArr = [
[1,1,1,0,0,0],
[0,1,0,0,0,0],
[1,1,1,0,0,0],
[0,0,2,4,4,0],
[0,0,0,2,0,0],
[0,0,1,2,4,0]
]
hourglassSum(inputArr)