We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

This is the solution I found, i had the loop stop at +2 away from the current index instead of a maxX/maxY, and saved the sums of all the hourglasses in an array which was a unnesscary decision after looking at your code, my variable names are also very long which i think i should change, everyone else seems to use very short variables.

function hourglassSum(arr) {
let top, mid, bottom = 0
//looking back idk why i thought these were pyramids!!!
let pyramid = []
let innerPyramid= []
function inOrder(a,b) {
return a - b
}
// outer loop moves up and down
for (const [index, value] of arr.entries()) {
if(index+2 < arr.length){
//inner loop goes left to right
for(const [innerIndex, innerValue] of value.entries()){
if(innerIndex + 2 < value.length){
//get the sum of the top
top = innerValue + value[innerIndex+1] + value[innerIndex+2]
//get the sum of the mid
mid = arr[index+1][innerIndex+1]
//get the sum of the bottom
bottom = arr[index+2][innerIndex]+arr[index+2][innerIndex+1]+arr[index+2][innerIndex+2]
//get the sum of the entire hourglass
innerPyramid[innerIndex] = top+mid+bottom
}else{
break
}
}
//combine the two arrays
Array.prototype.push.apply(pyramid, innerPyramid)
}else{
break
}
}
return pyramid.sort(inOrder)[pyramid.length - 1]
}

## 2D Array - DS

You are viewing a single comment's thread. Return to all comments →

This is the solution I found, i had the loop stop at +2 away from the current index instead of a maxX/maxY, and saved the sums of all the hourglasses in an array which was a unnesscary decision after looking at your code, my variable names are also very long which i think i should change, everyone else seems to use very short variables.