Write a dispatch algorithm in JavaScript. Watch it succeed—or strand passengers on floor 7. A classic coordination puzzle, now with real-time feedback and nowhere to hide.
Learn how to write algorithms that control elevators to transport passengers efficiently.
Your algorithm controls elevators to pick up waiting passengers and deliver them to their destinations. The goal is to minimize wait times and maximize throughput.
The init and update functions are optional. You can rely entirely on events if you prefer.
{ init: function(elevators, floors, store) { // Called once when simulation starts // Set up event handlers and initial state elevators.forEach(elevator => { elevator.on('idle', function() { // Elevator has no destinations - what should it do? }); elevator.on('floorButtonPressed', function(floorNum) { // A passenger inside pressed a floor button elevator.goToFloor(floorNum); }); }); floors.forEach(floor => { floor.on('callButtonPressed', function(direction) { // Someone pressed the call button on this floor }); }); }, update: function(dt, elevators, floors, store) { // Called every tick - use for periodic logic } }
The store is persistent key-value storage that persists across ticks. Use it to coordinate between elevators and track global state.
{ id: "120-4821", origin: 3, destination: 7, spawnTick: 120, pickupTick: 140, arrivalTick: 200, state: "waiting", // "arriving" | "waiting" | "riding" | "arrived" direction: "up" }
{ numElevators: 3, numFloors: 10, spawnRate: 20, // percent chance per tick seed: 42, maxTicks: 2000 }
Seeded RNG: Math.random() and rand() are deterministic per simulation seed.
Deterministic clock: Date.now(), new Date(), and performance.now() use a tick-based clock (tick * 1000 ms) with UTC formatting.
Runtime guard: init, update, and event handlers must finish within 50ms.
Reset required: Editing code or config marks the simulation stale. Hit Reset to apply changes.
Console output: console.log()/warn()/error() appear in the event log.
Available globals: Math, rand, Date, performance, Number, String, Array, Object, JSON, Boolean, parseInt, parseFloat, isNaN, isFinite.
isFull() before picking up more passengersgoToFloor(n, true) for sorted insertionTransported: Total passengers delivered to their destination.
Average Wait: Mean time from spawn to pickup (lower is better).
Max Wait: Longest any passenger waited (minimize this!).
Waiting: Current passengers waiting at floors.
Riding: Current passengers in elevators.
Algorithms run on 1,000 canonical seeds. Lower scores rank higher.
wait_per_seed = average(pickupTick - spawnTick for each transported passenger) final_score = average(wait_per_seed over all canonical seeds)