• + 0 comments

    JS

    function main() {
    
        let arr = Array(6);
    
        for (let i = 0; i < 6; i++) {
            arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
        }
        
        var hgSum = -9 * 7; //smallest possible hourglass sum
        
        //loop through the lines until the hourglass is reaching the bottom
        for (let l = 0; l < arr.length-2; l++){
            const arrLine = arr[l];
            
            //loop through the line until the top of the hourglass reaches the end of the line
            for (let i = 0; i < arr[l].length - 2; i++){
                var hourglass = arrLine.slice(i,i+3); //hourglass top
                hourglass.push(arr[l+1][i+1]); //hourglass waist
                hourglass.push(...arr[l+2].slice(i,i+3));//hourglass bottom
                // console.log(hourglass);
                
                var tempSum = hourglass.reduce((a,b) => a + b);
                // console.log(tempSum);
                if (tempSum > hgSum){
                    hgSum = tempSum;
                    }
                }
            }
            console.log(hgSum);
        }