Skip to content

Instantly share code, notes, and snippets.

@gangstead
Last active August 29, 2015 14:14
Show Gist options
  • Save gangstead/f30c2795f9f44f70095e to your computer and use it in GitHub Desktop.
Save gangstead/f30c2795f9f44f70095e to your computer and use it in GitHub Desktop.
{
init: function(elevators, floors) {
console.log('elev:' + elevators.length + ' flrs:'+floors.length);
elevators.forEach(function(elevator,index){
elevator.on("idle", function() {
console.log(index + " the devil's plaything");
var idleFloor = [0,0,0,0,0,7,0,0,0,0];
goToFloorSafe(elevator,idleFloor[index]);
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
});
elevator.on("floor_button_pressed", function(floorNum) {
if(goToFloorSafe(elevator,floorNum)){
var goingUp = elevator.destinationQueue[0] > elevator.currentFloor();
elevator.destinationQueue.sort(); //lowest floor next
var safety = floors.length;
if(goingUp) {
//we're headed up, lowest floor is next in queue. Make it so lowest floor above is next
while(elevator.destinationQueue[0] < elevator.currentFloor() && safety) {
frontToBack(elevator.destinationQueue);
safety--;
}
} else {
elevator.destinationQueue.reverse();
//headed down, highest floor is next in queue, make it so highest floor lower than current is next
while(elevator.destinationQueue[0] > elevator.currentFloor() && safety) {
frontToBack(elevator.destinationQueue);
safety--;
}
}
} else { console.log(index + ' already headed to '+ floorNum);}
if(_.uniq(elevator.destinationQueue).length != elevator.destinationQueue.length) {
console.log(index + ':'+elevator.currentFloor()+' q:'+elevator.destinationQueue);
console.log(elevator);
}
elevator.checkDestinationQueue();
});
elevator.on("stopped_at_floor", function(floorNum){
var nextFloor = elevator.destinationQueue[0];
if(elevator.destinationQueue.length == 0 || elevator.loadFactor() == 0) {
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(true);
}
else if(nextFloor > floorNum){
elevator.goingUpIndicator(true);
elevator.goingDownIndicator(false);
} else {
elevator.goingUpIndicator(false);
elevator.goingDownIndicator(true);
}
});
elevator.on("passing_floor",function(floorNum,direction){
var headedThereAnyways = elevator.destinationQueue.indexOf(floorNum);
if(!floors[floorNum]) {console.log('WHHHAAAATTTT FLOOR '+floorNum); console.log(floors); return;} //this was happening for some reason
var goingRequestedDirection = floors[floorNum].buttonStates[direction];
if( (headedThereAnyways > 0) && goingRequestedDirection) {
//queue out of order. This elevator was going to stop at this floor anyways but not the next stop
console.log(index+ ' before splice:'+ elevator.destinationQueue + ' floorNum:'+floorNum);
elevator.destinationQueue.splice(headedThereAnyways,1);
elevator.destinationQueue.unshift(floorNum);
console.log(index+ ' after splice:' + elevator.destinationQueue);
elevator.checkDestinationQueue();
} else if(headedThereAnyways == -1 && goingRequestedDirection && elevator.loadFactor() < .5 ) {
console.log('watch out for hop ons');
//we have room to pick up someone and we're headed in the direction they want to go and we weren't right about to stop at that floor anyways
elevator.destinationQueue.unshift(floorNum);
elevator.checkDestinationQueue();
}
});
});
function leastFull(goingUp) {
var sameDirection = elevators.filter(function(elev){ return goingUp? elev.velocityY > 0 : elev.velocityY < 0});
if(sameDirection.length == 0) sameDirection = elevators.slice();
return sameDirection.sort(function(a,b){return a.loadFactor() - b.loadFactor()})[0]; //switch to most full for fewer moves and greater wait time
};
function frontToBack(arr){
arr.push(arr.shift());
};
floors.forEach(function(floor,index){
floor.on("up_button_pressed", function(event) {
goToFloorSafe(leastFull(true),index);
});
floor.on("down_button_pressed", function(event) {
goToFloorSafe(leastFull(false),index);
});
});
function goToFloorSafe(el, floorNum){
if(el.destinationQueue.indexOf(floorNum) == -1 ) {
el.goToFloor(floorNum);
return true;
} else {
return false;
}
}
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
@gangstead
Copy link
Author

Level 17 stats:

Level 17
Transported 1997
Elapsed time 1006s
Transported/s 1.99
Avg waiting time 7.6s
Max waiting time 36.5s
Moves 8770

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