Last active
August 29, 2015 14:22
-
-
Save kkoch986/7741bde0eebc60c3f255 to your computer and use it in GitHub Desktop.
A simulation of 2 solutions to the prisoners with the light switch problem
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
function solve(prisoner_count, hard_limit) { | |
var prisoners = []; | |
var startTime = ((new Date()).getTime()); | |
for(var x = 0 ; x < prisoner_count ; x++) { | |
prisoners[x] = false; | |
} | |
// Pick a random prisoner | |
function pickPrisoner() { | |
return Math.floor(Math.random() * prisoner_count); | |
} | |
var leader = pickPrisoner(); | |
var leaderSwitchCount = 0; | |
var prisonerHasSwitchedOnLight = {}; | |
var switchOnSlowApproach = false; | |
var switchOnFastApproach = false; | |
var day = 0; | |
var dayAllWereLoaded = null; | |
var fastSolved = false; | |
var slowSolved = false; | |
var window = {}; | |
var quiet = true; | |
while(!fastSolved || !slowSolved) { | |
day++; | |
var p = pickPrisoner(); | |
if(!quiet) { | |
console.log("Day " + day + | |
": Prisoner #" + p + | |
", light is " + (switchOnSlowApproach ? "ON" : "OFF") + | |
(dayAllWereLoaded ? " " + (day - dayAllWereLoaded) + " days late (all loaded on day "+dayAllWereLoaded+")" : "") | |
); | |
} | |
prisoners[p] = true; | |
if(!fastSolved) { | |
if(p === leader) { | |
if(switchOnFastApproach) { | |
leaderSwitchCount++; | |
if(leaderSwitchCount === prisoner_count - 1) { | |
fastSolved = day; | |
} | |
switchOnFastApproach = false; | |
} | |
switchOnFastApproach = false; | |
} else if(!prisonerHasSwitchedOnLight[p] && !switchOnFastApproach) { | |
switchOnFastApproach = true; | |
prisonerHasSwitchedOnLight[p] = true; | |
} | |
} | |
if(!slowSolved) { | |
if(window[p]) { | |
switchOnSlowApproach = true; | |
} | |
window[p] = true; | |
if(day !== 0 && day % prisoner_count === 0) { | |
if(switchOnSlowApproach === false) { | |
slowSolved = day; | |
} | |
switchOnSlowApproach = false; | |
window = {}; | |
} | |
// check if all were loaded. | |
if(dayAllWereLoaded === null) { | |
var allLoaded = true; | |
for(var p in prisoners) { | |
if(!prisoners[p]) { | |
allLoaded = false; | |
} | |
} | |
if(allLoaded) { | |
dayAllWereLoaded = day; | |
} | |
} | |
} | |
if(day > hard_limit) { | |
break ; | |
} | |
} | |
return { | |
"slow":slowSolved, | |
"fast":fastSolved, | |
"actual": dayAllWereLoaded, | |
"count":prisoner_count, | |
"time": ((new Date()).getTime() - startTime) | |
} | |
} | |
var counts = [ | |
5, | |
10, | |
25, | |
50, | |
100, | |
200, | |
400 | |
]; | |
var hard_limit = 365000000; // if they dont get out in a million years, give up? | |
var iterations_per_count = 20; | |
console.log("prisoners,actual,slow,fast,sim_time_seconds"); | |
for(var x = 0 ; x < counts.length ; x++) { | |
for(var y = 0 ; y < iterations_per_count ; y++) { | |
var res = solve(counts[x], hard_limit); | |
console.log(res.count +","+res.actual+","+res.slow+","+ res.fast+","+(res.time / 1000.0)); | |
} | |
} | |
// console.log(prisoner_count + ", " + slowSolved + " total days for certainty in slow algorithm, " + fastSolved + ", for fast algorithm " + dayAllWereLoaded + " days in reality."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment