Last active
June 3, 2022 02:57
-
-
Save efouts/0cbbfa125519ef83b27d to your computer and use it in GitHub Desktop.
Passes All Challenges (some take a number of tries)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
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) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
really nice solution