Created
October 9, 2025 11:12
-
-
Save roeniss/1ce26a8a9347b40e2b21d4eecd9764f0 to your computer and use it in GitHub Desktop.
elevator saga my code
This file contains hidden or 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
| { | |
| state: { | |
| waiting: { | |
| "up" : {}, | |
| "down": {}, | |
| }, | |
| highestFloor: 0, | |
| totalWaitingUp: function() { | |
| return Object.values(this.state.waiting["up"]).reduce((a,b) => a+b, 0) | |
| }, | |
| lowestWaitingFloor: function(direction) { | |
| return Object.entries(this.waiting[direction]) | |
| .filter(([floorNum, count]) => count > 0) | |
| .reduce((min, [k]) => (min === undefined) ? k : (k < min ? k : min), undefined); | |
| }, | |
| totalWaitingDown: function() { | |
| return Object.values(this.state.waiting["down"]).reduce((a,b) => a+b, 0) | |
| }, | |
| }, | |
| initState: function(elevators, floors) { | |
| floors.forEach ((f) => { | |
| this.state.waiting["up"][f.floorNum()] = 0 | |
| this.state.waiting["down"][f.floorNum()] = 0 | |
| }) | |
| this.state.highestFloor = floors.length | |
| }, | |
| init: function(elevators, floors) { | |
| this.initState(elevators, floors) | |
| const e0 = elevators[0] | |
| const e1 = elevators[1] | |
| elevators.forEach (e => { | |
| if (e === e0){ | |
| } | |
| e.on("idle", () => { | |
| const interval = setInterval(() => { | |
| const lowestUpFloorNum = this.state.lowestWaitingFloor("up"); | |
| console.log("lowestUpFloorNum:", lowestUpFloorNum) | |
| if (lowestUpFloorNum) { | |
| e.goToFloor(lowestUpFloorNum); | |
| clearInterval(interval); | |
| return; | |
| } | |
| const lowestDownFloorNum = this.state.lowestWaitingFloor("down"); | |
| console.log("lowestDownFloorNum:", lowestDownFloorNum) | |
| if (lowestDownFloorNum) { | |
| e.goToFloor(lowestDownFloorNum); | |
| clearInterval(interval); | |
| return; | |
| } | |
| }, 10); | |
| }); | |
| e.on("floor_button_pressed", (floorNum) => { | |
| if(e.destinationDirection() === "stopped"){ | |
| e.goToFloor(floorNum) | |
| } | |
| }) | |
| e.on("passing_floor", (floorNum, direction) => { | |
| const waitingNum = this.state.waiting[direction][floorNum] | |
| if(waitingNum > 0 && e.loadFactor() < 0.9){ | |
| e.goToFloor(floorNum) | |
| e.destinationQueue.sort() | |
| if(direction === "down"){ | |
| e.destinationQueue.sort().reverse() | |
| } | |
| e.checkDestinationQueue(); | |
| } | |
| }) | |
| e.on("stopped_at_floor", (floorNum) => { | |
| this.state.waiting["up"][floorNum] = 0 | |
| this.state.waiting["down"][floorNum] = 0 | |
| }) | |
| }) | |
| floors.forEach(f => { | |
| f.on("up_button_pressed", () => { | |
| this.state.waiting["up"][f.floorNum()]++; | |
| }) | |
| f.on("down_button_pressed", () => { | |
| this.state.waiting["down"][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
With 50+ retrials for each round, I could pass 1 to 11 challenges.