Skip to content

Christian Genco's hacky solution to every "N elevator moves or less" level

Christian Genco edited this page Jan 23, 2015 · 4 revisions

illustration of this code being run

Alternate title: The Chinese Subway

This algorithm only uses the first elevator, waits ten seconds before moving (so the queue at every floor fills up), then moves up and down stopping at each floor.

In practice it beats the "N elevator moves or less" levels with considerable margin to spare.

{
    init: function(elevators, floors) {
        // first, wait ten seconds for the queues to fill up on each floor
        setTimeout(function(){
            // use only the first elevator
            var e = elevators[0];
            
            // ...and open up its doors on the ground floor
            e.goToFloor(0);
            
            var direction = 1;
            e.on('idle', function(){
                // are we on the top floor? go down
                if(e.currentFloor() >= floors.length - 1) direction = -1;
                
                // are we on the ground floor? go up next time
                if(e.currentFloor() <= 0) direction = 1;
                
                // go to the next floor!
                e.goToFloor(e.currentFloor() + direction);
            });
        }, 10000);
    },
    update: function(dt, elevators, floors) {}
}
Clone this wiki locally