Created
November 5, 2016 11:07
-
-
Save joelhinz/ae8edc1ffdaa54aa143ccd321cae2b07 to your computer and use it in GitHub Desktop.
The twelve prisoners, shorter version
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
var _ = require('lodash'); | |
function run() { | |
var piles = _.chunk(_.shuffle(_.range(52).map(i => Math.floor(i/4))), 4); | |
return iterate(piles[0].shift(), piles); | |
} | |
function iterate(hold, piles) { | |
if (_.range(1, 13).every(i => piles[i].length === 0)) return true; | |
if (piles[0].length == 0 && hold === 0) return false; | |
return iterate(piles[hold].shift(), piles); | |
} | |
var data = _.range(100000000).reduce(i => run() ? [i[0] + 1, i[1]] : [i[0], i[1] + 1], [0, 0]); | |
console.log(`W: ${data[0]}\nL: ${data[1]}\n%: ${(data[0] / (data[0] + data[1]) * 100).toFixed(2)}%`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment