Skip to content

Instantly share code, notes, and snippets.

@AlexanderFadeev
Last active July 8, 2020 06:45
Show Gist options
  • Save AlexanderFadeev/7b8edf6440d0d5e28f573dce9d121aac to your computer and use it in GitHub Desktop.
Save AlexanderFadeev/7b8edf6440d0d5e28f573dce9d121aac to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
let tasks = [];
for (let index in floors) {
let floor = floors[index];
floor.on("up_button_pressed", () => {
tasks.push({
floor: floor.floorNum(),
dir: "up",
});
});
floor.on("down_button_pressed", () => {
tasks.push({
floor: floor.floorNum(),
dir: "down",
});
})
}
for (let index in elevators) {
let elevator = elevators[index];
elevator.dir = "up";
elevator.pickup = function() {
if (tasks.length === 0) {
return;
}
elevator.goToFloor(tasks[0].floor);
tasks.splice(0, 1);
};
elevator.on("idle", function() {
if (elevator.getPressedFloors().length == 0) {
elevator.pickup();
return;
}
if (elevator.dir == "up") {
let next = 9999;
for (let floor of elevator.getPressedFloors()) {
if (floor < elevator.currentFloor()) {
continue;
}
next = Math.min(next, floor);
}
elevator.goToFloor(next);
return;
} else {
let next = -1;
for (let floor of elevator.getPressedFloors()) {
if (floor > elevator.currentFloor()) {
continue;
}
next = Math.max(next, floor);
}
elevator.goToFloor(next);
return;
}
});
}
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
@AlexanderFadeev
Copy link
Author

Stuck at challenge 12

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