Skip to content

Instantly share code, notes, and snippets.

@renatocassino
Last active August 7, 2018 14:50
Show Gist options
  • Save renatocassino/93e174775d239aff35ce63e91cc285be to your computer and use it in GitHub Desktop.
Save renatocassino/93e174775d239aff35ce63e91cc285be to your computer and use it in GitHub Desktop.
getEmptyBlocks.js
const getEmptyBlocks = (board) => (
board.reduce((lineData, line, idxColumn) => { // Iterate over columns
const result = line.reduce((data, row, idx) => { // Iterate over lines
if (row !== 0) return data; // If value is different of zero, response last iterate
return [[idxColumn, idx], ...data]; // Join last iterate (or default value) with new data
}, []); // Default value to reduce in line
return [...lineData, ...result]; // Join old value with new value and return to next reduce
}, []) // Default value to reduce in column
);
const board = [
[2, 2, 2, 2],
[0, 4, 16, 16],
[8, 8, 0, 32],
[0, 0, 0, 64]
];
console.log(getEmptyBlocks(board));
// Response [ [ 1, 0 ], [ 2, 2 ], [ 3, 2 ], [ 3, 1 ], [ 3, 0 ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment