Created
August 22, 2019 18:23
-
-
Save krevels/812b7d4a96e9ce8fa4fae8567be099d2 to your computer and use it in GitHub Desktop.
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
class ballClock { | |
constructor() { | |
this.Min = []; | |
this.FiveMin = []; | |
this.Hour = []; | |
this.Main = []; | |
} | |
modeOneCycleLength(numberOfBalls) { | |
let available = new Array(numberOfBalls).fill(0).map((n, i) => i + 1); | |
let mins = Number.POSITIVE_INFINITY; | |
// initialize the ramps (queues) | |
let rampOne = []; | |
let rampTwo = []; | |
let rampThree = []; | |
// every minute that passes add one ball from the queue to the top ramp (rampOne) | |
// every fifth minute add to the second ramp (rampTwo) | |
// and every hour (60 minutes) add to the third ramp (rampThree) | |
for (let i = 1; i <= mins; i++) { | |
if (i % 5 === 0) { | |
rampTwo.push(available.pop()); | |
available = rampOne.concat(available); | |
rampOne.length = 0; | |
if (rampTwo.length === 12) { | |
rampThree.push(rampTwo.pop()); | |
available = rampTwo.concat(available); | |
rampTwo.length = 0; | |
} | |
if (rampThree.length === 12) { | |
let value = rampThree.pop(); | |
available = [value].concat(rampThree).concat(available); | |
rampThree.length = 0; | |
if (available.length === numberOfBalls) { | |
// only need to check for cycle if all balls are returned to pool | |
let count = 0; | |
for (let j = 0; j < available.length; j++) { | |
if (j === available[j] - 1) { | |
count++; | |
} | |
} | |
// check if every value at each index is what it was initialized with | |
// if true, we have completed a cycle | |
if (count === available.length) { | |
return 'Cycle length:', i / 60 / 24; | |
} | |
} | |
} | |
} else { | |
rampOne.push(available.pop()); | |
} | |
} | |
} | |
modeTwoCurrentState(numberOfBalls, mins) { | |
let available = new Array(numberOfBalls).fill(0).map((n, i) => i + 1); | |
let rampOne = []; | |
let rampTwo = []; | |
let rampThree = []; | |
// iterations will stop once minutes value (passed in) is reached | |
for (let i = 1; i <= mins; i++) { | |
if (i % 5 === 0) { | |
rampTwo.push(available.pop()); | |
available = rampOne.concat(available); | |
rampOne.length = 0; | |
if (rampTwo.length === 12) { | |
rampThree.push(rampTwo.pop()); | |
available = rampTwo.concat(available); | |
rampTwo.length = 0; | |
} | |
if (rampThree.length === 12) { | |
let value = rampThree.pop(); | |
available = [value].concat(rampThree).concat(available); | |
rampThree.length = 0; | |
} | |
} else { | |
rampOne.push(available.pop()); | |
} | |
} | |
//state is recorded | |
this.Min = rampOne; | |
this.FiveMin = rampTwo; | |
this.Hour = rampThree; | |
this.Main = available; | |
return JSON.stringify(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment