Skip to content

Instantly share code, notes, and snippets.

@efouts
Last active June 3, 2022 02:57
Show Gist options
  • Select an option

  • Save efouts/0cbbfa125519ef83b27d to your computer and use it in GitHub Desktop.

Select an option

Save efouts/0cbbfa125519ef83b27d to your computer and use it in GitHub Desktop.
Passes All Challenges (some take a number of tries)
{
init: function(elevators, floors) {
elevators.forEach(function(e) {
e.isDestination = function(floorNum) {
return e.destinationQueue.indexOf(floorNum) != -1;
}
e.on("floor_button_pressed", function(floorNum) {
if (!e.isDestination(floorNum))
e.goToFloor(floorNum);
});
e.on("passing_floor", function(floorNum, direction) {
if (e.isDestination(floorNum)) {
e.destinationQueue = e.destinationQueue.filter(function(f) { return f != floorNum; });
e.goToFloor(floorNum, true);
}
});
});
floors.forEach(function(f) {
f.on("up_button_pressed down_button_pressed", function() {
if (elevators.some(function(e) { return e.isDestination(f.floorNum()); }))
return;
var e = elevators[0];
for(var i = 0; i < elevators.length; i++)
if (elevators[i].destinationQueue.length < e.destinationQueue.length)
e = elevators[i];
if (!e.isDestination(f.floorNum()))
e.goToFloor(f.floorNum());
});
});
},
update: function(dt, elevators, floors) { }
}
@zkendall
Copy link
Copy Markdown

Your creation and usage of isDestination is a nice touch.

@kbrock
Copy link
Copy Markdown

kbrock commented Jul 10, 2019

really nice solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment