Skip to content

Instantly share code, notes, and snippets.

@kenglxn
Created April 15, 2016 12:17
Show Gist options
  • Save kenglxn/1637f9c9239462c68869766736a41746 to your computer and use it in GitHub Desktop.
Save kenglxn/1637f9c9239462c68869766736a41746 to your computer and use it in GitHub Desktop.
my solution to http://play.elevatorsaga.com/ #wip
{
operator: function(elevators) {
//console.log("create operator", elevators);
var distance = function (floor, elevatorFloor) {
return Math.abs(floor - elevatorFloor);
};
var isClosest = function (fromFloor) {
return function (e) {
return distance(fromFloor, e.currentFloor());
};
};
var operator = {
up_button_pressed: function (fromFloor) {
operator.floor_button_pressed(fromFloor, "up");
},
down_button_pressed: function (fromFloor) {
operator.floor_button_pressed(fromFloor, "down");
},
floor_button_pressed: function (fromFloor, direction) {
var floorNum = fromFloor.floorNum();
console.log("floor button pressed: ", floorNum);
var nonFullElevators = _.reject(elevators, function(e) { return e.loadFactor === 1; });
var idleElevators = _.sortBy(nonFullElevators, isClosest(fromFloor)).filter(function (e) { return e.destinationQueue.length === 0; });
var closestElevators = _.sortBy(nonFullElevators, isClosest(fromFloor));
var incomingElevators = elevators.filter(function (e) { return e.destinationQueue.includes(floorNum); });
var elevatorGoingThatWay = [].concat(closestElevators, incomingElevators).find(function (e) {
return e.destinationQueue.find(function (dest) {
return (direction === "up" && dest > fromFloor) || (direction === "down" && dest < fromFloor);
});
});
if (!_.isEmpty(idleElevators)) {
console.log("grab idle elevator");
operator.fetch(idleElevators[0], floorNum);
return;
}
if (elevatorGoingThatWay) {
console.log("elevator going the right way");
operator.fetch(elevatorGoingThatWay, floorNum);
return;
}
if (!_.isEmpty(incomingElevators)) {
// if (distance(fromFloor, incoming.currentFloor) < distance(fromFloor, closestElevator.currentFloor)) {
// console.log("incoming but elevator is closer");
// operator.fetch(idleElevator, floorNum);
// return;
// } else {
console.log("incoming");
return;
// }
}
if (!_.isEmpty(closestElevators)) {
console.log("grab closest elevator");
operator.fetch(closestElevators[0], floorNum);
return;
}
console.error("did not get any elevator", nonFullElevators);
},
fetch: function (elevator, num) {
var currentFloor = elevator.currentFloor();
console.log("fetch elevator from %s to %s ", currentFloor, num);
var insertIndex = 0;
for (insertIndex; insertIndex < elevator.destinationQueue.length; insertIndex++) {
var destination = elevator.destinationQueue[insertIndex];
var isBetween =
(currentFloor < num && num < destination) ||
(currentFloor > num && num > destination);
if (isBetween || destination === num) {
break;
}
}
elevator.destinationQueue.splice(insertIndex, 0, num);
elevator.checkDestinationQueue();
console.log("added floor %s to queue", num, elevator.destinationQueue);
}
};
return operator;
},
init: function(elevators, floors) {
var operator = this.operator(elevators);
floors.forEach(function (floor) {
floor.on("up_button_pressed", operator.up_button_pressed);
floor.on("down_button_pressed", operator.down_button_pressed);
});
var disperseFacor = floors.length / elevators.length;
elevators.forEach(function (elevator, index) {
elevator.goToFloor(disperseFacor * index, true);
});
elevators.forEach(function (elevator, index) {
elevator.on("floor_button_pressed", function (num) {
operator.fetch(elevator, num);
});
elevator.on("stopped_at_floor", function (num) {
console.log("stopped at floor %s", num, elevator.destinationQueue, elevator);
});
});
},
update: function(dt, elevators, floors) {
// We normally don't need to do anything here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment