Skip to content

Instantly share code, notes, and snippets.

@j-quelly
Created August 13, 2019 18:20
Show Gist options
  • Save j-quelly/7ab12f4283423fbb0c065b3e9925712d to your computer and use it in GitHub Desktop.
Save j-quelly/7ab12f4283423fbb0c065b3e9925712d to your computer and use it in GitHub Desktop.
count clouds
function sweepClouds({ skyMap, y, x }) {
if (!skyMap[y]) {
return;
}
if (!skyMap[y][x]) {
return;
}
if (skyMap[y][x] === '1') {
skyMap[y][x] = '0';
sweepClouds({ skyMap, y: y - 1, x }); // top
sweepClouds({ skyMap, y, x: x + 1 }); // right
sweepClouds({ skyMap, y: y + 1, x }); // bottom
sweepClouds({ skyMap, y, x: x - 1 }); // left
}
return skyMap;
}
function countClouds(skyMap) {
let clouds = 0;
skyMap.forEach((node, y) => {
node.forEach((tile, x) => {
if (tile === '1') {
clouds++;
}
skyMap = sweepClouds({ y, x, skyMap })
})
});
return clouds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment