Skip to content

Instantly share code, notes, and snippets.

@yock yock/
Last active Apr 13, 2016

What would you like to do?

Elevator Saga

A programming challenge for dispatching elevators. If you've ever waited for an elevator and thought about how much sorter the wait would be if you were the programmer, give this a try.

About the Algorithm

This very simplistic implementation attempts to do just two things:

  1. Always choose the elevator car with the fewest queued destinations.
  2. Never push a destination onto the queue if it's already there.

When ran against their examples it's clear that the first elevator is constantly working hard while the remaining elevators sit mostly idle. A naive round-robin approach might work better than attempting to identify nearly-idle elevators.

About the Implementation

init: function(elevators, floors) {
_.each(elevators,function(elevator) {
if(!elevator.destinationQueue.includes(floorNum)) {
_.each(floors, function(floor) {
floor.on('up_button_pressed down_button_pressed', function() {
var assigned = _.sortBy(elevators, function(elevator) {
var floorNum = floor.floorNum();
if(!assigned.destinationQueue.includes(floorNum)) {
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.