Created
March 14, 2024 13:40
-
-
Save BiosBoy/c19024f8aa41c38e8d3c8813b05de656 to your computer and use it in GitHub Desktop.
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
const arr = [ | |
[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], | |
]; | |
const SHIFT_MAX = 3; | |
const MAX_ROWS = 4; | |
const MAX_COLUMNS = 4; | |
const DENIED_CLOCK_IDX = [3, 5]; | |
const solver = (data) => { | |
const matrixArr = []; | |
const nextArr = []; | |
const sumArr = []; | |
let currentRow = 0; | |
let currentColumn = 0; | |
const rowsMaxArr = [...Array(MAX_ROWS).keys()]; | |
const columnsMaxArr = [...Array(MAX_COLUMNS).keys()]; | |
rowsMaxArr.forEach((idx) => { | |
const currentRows = data.slice(currentRow, currentRow + SHIFT_MAX); | |
matrixArr.push(currentRows); | |
if (idx === rowsMaxArr.length - 1) { | |
currentRow = 0; | |
} else { | |
currentRow += 1; | |
} | |
}); | |
matrixArr.forEach((matrix) => { | |
columnsMaxArr.forEach((idx) => { | |
const getNewSlice = (id) => | |
matrix[id].slice(currentColumn, currentColumn + SHIFT_MAX); | |
nextArr.push([...getNewSlice(0), ...getNewSlice(1), ...getNewSlice(2)]); | |
if (idx === columnsMaxArr.length - 1) { | |
currentColumn = 0; | |
currentRow = 0; | |
} else { | |
currentColumn += 1; | |
currentRow += 1; | |
} | |
}); | |
}); | |
nextArr.forEach((matrix) => { | |
let sum = 0; | |
matrix.forEach((value, idx) => { | |
if (DENIED_CLOCK_IDX.includes(idx)) { | |
return; | |
} | |
sum += value; | |
}); | |
sumArr.push(sum); | |
}); | |
return Math.max(...sumArr); | |
}; | |
console.log(solver(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment