Skip to content

Instantly share code, notes, and snippets.

@kharioki
Created January 2, 2019 12:52
Show Gist options
  • Save kharioki/ecf454a181ba50c747c69e16783b045a to your computer and use it in GitHub Desktop.
Save kharioki/ecf454a181ba50c747c69e16783b045a to your computer and use it in GitHub Desktop.
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
// Complete the diagonalDifference function below.
function diagonalDifference(arr) {
let n = arr.length;
let diag1 = 0;
let diag2 = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
{ i === j && (diag1 += arr[i][j]) }
{ (i + j === n - 1) && (diag2 += arr[i][j]) }
}
}
return Math.abs(diag1 - diag2)
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const n = parseInt(readLine(), 10);
let arr = Array(n);
for (let i = 0; i < n; i++) {
arr[i] = readLine().split(' ').map(arrTemp => parseInt(arrTemp, 10));
}
const result = diagonalDifference(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