Skip to content

Instantly share code, notes, and snippets.

@GiuseppeMP
Created May 15, 2021 02:11
Show Gist options
  • Save GiuseppeMP/cb724a0e087f79aea41a54f7320c30a2 to your computer and use it in GitHub Desktop.
Save GiuseppeMP/cb724a0e087f79aea41a54f7320c30a2 to your computer and use it in GitHub Desktop.
'use strict';
import { WriteStream, createWriteStream } from "fs";
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString: string = '';
let inputLines: string[] = [];
let currentLine: number = 0;
process.stdin.on('data', function(inputStdin: string): void {
inputString += inputStdin;
});
process.stdin.on('end', function(): void {
inputLines = inputString.split('\n');
inputString = '';
main();
});
function readLine(): string {
return inputLines[currentLine++];
}
/*
* Complete the 'hourglassSum' function below.
*
* The function is expected to return an INTEGER.
* The function accepts 2D_INTEGER_ARRAY arr as parameter.
*
* 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
*/
function hourglassSum(arr: number[][]): number {
// Write your code here
const results : number[] = []
for(let i = 0; i <= 3; i++){
for(let j = 0; j <=3; j++){
results.push(hourglassSumHelper(arr, [i,j]))
}
}
return Math.max(... results)
}
function hourglassSumHelper(arr: number[][], begin: number[]) : number {
const idi = begin[0]
const idj = begin[1]
const sums = []
for(let i = 0; i <= 2; i++){
for(let j =0; j <=2; j++){
if((i === 1 && j === 1 ) || i !== 1)
sums.push(arr[idi+i][idj+j])
}
}
return sums.reduce( (a,b) => a+b)
}
function main() {
const ws: WriteStream = createWriteStream(process.env['OUTPUT_PATH']);
let arr: number[][] = Array(6);
for (let i: number = 0; i < 6; i++) {
arr[i] = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
}
const result: number = hourglassSum(arr);
ws.write(result + '\n');
ws.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment