devdating.net Solution

From http://devdating.net/

rocksRocks are falling from the sky! You get an array of columns: 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() {
var rows = map[0].length;
var cols = map.length;
String.prototype.replaceAt=function(index, character)
{
return this.substr(0, index) + character + this.substr(index+character.length);
}
var dropRocks = function(c)
{
for(var r = rows-1; r >= 0; r--)
{
if(map[c][r] == 'o' && map[c][r+1] == '.')
{
map[c] = map[c].replaceAt(r, '.');
map[c] = map[c].replaceAt(r+1, 'o');
r = rows-1;
}
}
}
var i = 0;
for(i = 0; i < cols; i++)
{
dropRocks(i);
}
console.dir(map);
return map;
})();

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.