JSFiddle - React, Tailwind, and code Playground

by Robodude

JavaScript

function hourglassSum(arr) {
    let max = 0;

    const relativeFlatIndex = [0, 1, 2, 7, 12, 13, 14]
    const relativeFlatIndex2 = [0, 2, 6, 7, 8, 12, 14]

    const flat = [].concat(...arr)
		
    for (let i = 0; i < flat.length; i++) {
				console.log("new one")
        let sum = 0;

        for (let j = 0; j < relativeFlatIndex.length; j++) {
            let indexOffset = relativeFlatIndex[j];

            let currentValue = flat[i + j];
						console.log(i, indexOffset, currentValue, sum);
            if (!currentValue && currentValue !== 0) {
                break;
            }
            else {
                sum = sum + currentValue;
            }

        }

        if (sum > max) {
            max = sum;
        }

    }

    return max;
}

const a = [
	[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]
]


console.log(hourglassSum(a))