Skip to content

Instantly share code, notes, and snippets.

@kietnguyen
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save kietnguyen/30eb082dc1adf5c48478 to your computer and use it in GitHub Desktop.

Select an option

Save kietnguyen/30eb082dc1adf5c48478 to your computer and use it in GitHub Desktop.
devdating.net Programming Challenge
/*
Rocks are falling from the sky! You get an array of rocks o and platforms #
Simulate rocks' flight until they hit a platform, another rock or the bottom. Platforms stay put.
map is an array in the format ['ooo###...', ...], where map[2][5] translates to the rock at x: 2, y: 5
return the result after all rocks have stopped moving.
*/
(function() {
return map.map(function(col) {
var count = 0,
len = col.length,
i, j;
// convert string to array to manipulate
col = col.split("");
for (i = 0, len = col.length; i < len; i++) {
if (col[i] === "o") {
// count # of rocks above a platform or bottom
col[i] = ".";
count++;
} else if (col[i] === "#") {
// stack up the rocks above a platform
for (j = 0; j < count; j++)
col[i-1-j] = "o";
count = 0;
}
}
// stack up the rocks above bottom
for (j = 0; j < count; j++)
col[len-1-j] = "o";
// convert back to original format
return col.join("");
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment