Created
March 15, 2017 09:29
-
-
Save eugenserbanescu/097691e58ce0d5978a1cea971896f6e0 to your computer and use it in GitHub Desktop.
largest product of 4 numbers horizontally, vertically or diagonally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let colLarge = [ | |
[1,1,1,1,1,4], | |
[1,1,1,1,1,4], | |
[1,1,1,1,1,4], | |
[1,1,1,1,1,4], | |
[1,1,1,1,1,1], | |
[1,1,1,1,1,1] | |
] | |
let rowLarge = [ | |
[1,1,1,1,1,1], | |
[1,1,1,1,1,1], | |
[1,1,1,1,1,1], | |
[1,1,5,5,5,5], | |
[1,1,1,1,1,1], | |
[1,1,1,1,1,1] | |
] | |
let diagLarge = [ | |
[1,1,1,1,1,1], | |
[1,6,1,1,1,1], | |
[1,1,6,1,1,1], | |
[1,1,1,6,1,1], | |
[1,1,1,1,6,1], | |
[1,1,1,1,1,1] | |
] | |
function findLargest(data) { | |
let largest = 0; | |
for(var i = 0; i < data.length; i++) { | |
for(var j = 0; j < data[i].length; j++) { | |
let horizontal = vertical = diagonal = data[i][j]; | |
for(var k = 1; k < 4; k++) { | |
if(j < data[i].length - 3) horizontal = horizontal * data[i][j + k]; | |
if(j < data[i].length - 3 && i < data.length - 3) diagonal = diagonal * data[i + k][j + k]; | |
if(i < data.length - 3) vertical = vertical * data[i + k][j]; | |
} | |
if(horizontal > largest) largest = horizontal; | |
if(diagonal > largest) largest = diagonal; | |
if(vertical > largest) largest = vertical; | |
} | |
} | |
return largest; | |
} | |
const col = findLargest(colLarge); | |
const row = findLargest(rowLarge); | |
const diag = findLargest(diagLarge); | |
console.log(col); | |
console.log(row); | |
console.log(diag); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment