Last active
August 29, 2015 14:22
-
-
Save kietnguyen/30eb082dc1adf5c48478 to your computer and use it in GitHub Desktop.
devdating.net Programming Challenge
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
| /* | |
| 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