Created
August 13, 2019 18:20
-
-
Save j-quelly/7ab12f4283423fbb0c065b3e9925712d to your computer and use it in GitHub Desktop.
count clouds
This file contains hidden or 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
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