Skip to content

Instantly share code, notes, and snippets.

@ironchefpython
Last active January 1, 2016 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ironchefpython/8174134 to your computer and use it in GitHub Desktop.
Save ironchefpython/8174134 to your computer and use it in GitHub Desktop.
// Fill the empty space with the minimum number of rectangles.
// The grid size is 1 meter, but the smallest wall/floor tile is 4 meters.
// If you can do better than one rectangle for every tile, let us know!
// We'll help you find a programming job (if you want one).
// Check the guide for more info, and press Contact below to report success.
var ar = this.addRect;
var tileSize = 4;
function draw(x, y, w, h) {
w = w * tileSize;
h = h * tileSize;
ar(x*tileSize+w/2, y*tileSize+h/2, w, h);
}
function occupied(x,y) {
return grid[y*tileSize][x*tileSize].length > 0;
}
var grid = this.getNavGrid().grid;
var curLines = [];
for(var y = 0; (y+1) * tileSize < grid.length; y += 1) {
var lastLines = curLines;
curLines = [];
for(var x = 0; (x+1) * tileSize < grid[0].length; x += 1) {
if(!occupied(x,y)) {
var startx = x;
while(!occupied(x,y)) {
x += 1;
}
var line = [startx, y, x-startx];
var i = lastLines.length;
while(i--){
var last = lastLines[i];
if (last[0] === line[0] && last[2] === line[2]) {
line = last;
lastLines.splice(i, 1);
break;
}
}
curLines.push(line);
}
}
var j = lastLines.length;
while(j--) {
var l = lastLines[j];
draw(l[0], l[1], l[2], y-l[1]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment