Created
August 9, 2019 05:18
-
-
Save nitobuendia/e0a4942a5e483eac100a3a6eed35a761 to your computer and use it in GitHub Desktop.
Solution to the cuddly duddly fuddly wuddly riddle - https://www.youtube.com/watch?v=z-ZEfxAL9SI
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
function selectEggs(addEgg) { | |
let blue = 23; | |
let purple = 33; | |
let red = 43; | |
if (addEgg == 'blue') blue++; | |
else if (addEgg == 'purple') purple++; | |
else if (addEgg == 'red') red++; | |
else { | |
console.log('Must select a colour: blue, purple, red'); | |
return; | |
} | |
const makePurple = () => { | |
blue--; | |
purple++; | |
red--; | |
}; | |
const makeRed = () => { | |
blue--; | |
purple--; | |
red++; | |
}; | |
const makeBlue = () => { | |
blue++; | |
purple--; | |
red--; | |
}; | |
let chosenColor = ''; | |
while (true) { | |
// Pure combination. | |
if (blue < purple && blue < red) { | |
makeBlue(); | |
} else if (red < purple && red < blue) { | |
makeRed(); | |
} else if (purple < blue && purple < red) { | |
makePurple(); | |
// Random combinations. | |
} else if (blue > purple && purple == red) { | |
if (Math.random() > 0.5) makeRed(); | |
else makePurple(); | |
} else if (red > purple && purple == blue) { | |
if (Math.random() > 0.5) makeBlue(); | |
else makePurple(); | |
} else if (purple > blue && blue == red) { | |
if (Math.random() > 0.5) makeRed(); | |
else makeBlue(); | |
} else if(purple == blue && blue == red) { | |
const r = Math.random(); | |
if (r < (1/3)) makeRed(); | |
else if (r < (2/3)) makeBlue(); | |
else makePurple(); | |
} | |
if (blue > 0 && purple == 0 && red == 0) { | |
chosenColor = 'blue'; | |
break; | |
} else if (red > 0 && purple == 0 && blue == 0) { | |
chosenColor = 'red'; | |
break; | |
} else if (purple > 0 && blue == 0 && red == 0) { | |
chosenColor = 'purple'; | |
break; | |
} else if (red < 0 || blue < 0 || purple < 0) { | |
console.log('Bug found'); break; | |
} | |
} | |
console.log(blue, purple, red, chosenColor); | |
return chosenColor; | |
} | |
function runExperiment(addEgg, nExperiments = 100) { | |
if (addEgg != 'blue' && addEgg != 'purple' && addEgg == 'red') { | |
console.log('Must select a colour: blue, purple, red'); | |
return; | |
} | |
const solution = {}; | |
for (i = 0; i < nExperiments; i++) { | |
const chosenColor = selectEggs(addEgg); | |
if (!chosenColor in solution) solution[chosenColor] = 0; | |
solution[chosenColor]++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment