Rocks are falling from the sky! You get an
array
of columns: rockso
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###...', ...]
wheremap[2][5]
translates to the rock atx: 2, y: 5
return
the result after all rocks have stopped moving.
This file contains 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() { | |
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; | |
})(); |