Created
March 12, 2016 09:36
-
-
Save voltrevo/27e48c2ba3e17fab7982 to your computer and use it in GitHub Desktop.
Computer assisted proof for a 2+/3 prisoner survival strategy for https://www.youtube.com/watch?v=7hJ4Azr--s8
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
'use strict'; | |
const assert = (x) => { | |
if (!x) { | |
throw new Error('Assertion failure'); | |
} | |
}; | |
const range = (n) => (new Array(n)).fill(0).map((x, i) => i); | |
const strategy = (responses, visibleHats, log) => { | |
const totalHats = responses.length + visibleHats.length + 2; | |
assert(totalHats === 4); | |
const eliminatedHats = [...responses, ...visibleHats]; | |
const possibilities = range(totalHats).filter(i => eliminatedHats.indexOf(i) === -1); | |
log(`possibilities: ${possibilities.join(', ')}`); | |
if (responses.length === 0) { | |
log('I\'m the first prisoner'); | |
const nextHat = visibleHats[0]; | |
const lastHat = visibleHats[1]; | |
const rotation = [nextHat, ...possibilities].sort(); | |
log(`rotation is: ${rotation.join(', ')}`); | |
const nextHatIndex = rotation.indexOf(nextHat); | |
const [offset, lastHatParity, direction] = (lastHat % 2 === 1 ? | |
[1, 'odd', 'up'] : | |
[-1, 'even', 'down'] | |
); | |
const guess = rotation[(rotation.length + nextHatIndex + offset) % rotation.length]; | |
log( | |
`lastHat(${lastHat}) is ${lastHatParity}, so I will guess *${direction}* from ` + | |
`nextHat(${nextHat}) to get ${guess}` | |
); | |
return guess; | |
} else if (responses.length === 1) { | |
log('I\'m the middle prisoner'); | |
const lastHat = visibleHats[0]; | |
const rotation = [responses[0], ...possibilities].sort(); | |
log(`The first prisoner's rotation should have been: ${rotation.join(', ')}`); | |
const [offset, lastHatParity, direction] = (lastHat % 2 === 1 ? | |
[-1, 'odd', 'down'] : | |
[1, 'even', 'up'] | |
); | |
const firstResponseIndex = rotation.indexOf(responses[0]); | |
const guess = rotation[(rotation.length + firstResponseIndex + offset) % rotation.length]; | |
log( | |
`Since lastHat is ${lastHatParity}, I should go *${direction}* from the first prisoner's ` + | |
`guess ${responses[0]} to get ${guess}` | |
); | |
return guess; | |
} else if (responses.length === 2) { | |
log('I\'m the middle prisoner'); | |
log(`My possibilties are ${possibilities.join(', ')}`); | |
const scenarios = possibilities.map(myHat => ({ | |
myHat, | |
rotation: range(totalHats).filter(i => i !== myHat), | |
})); | |
log(`That means there are two scenarios: ${JSON.stringify(scenarios)}`); | |
const middleHat = responses[1]; | |
const scenariosConsistent = scenarios.map(({ myHat, rotation }) => { | |
log(`Scenario with my hat being ${myHat}:`); | |
const [offset, myHatParity, direction] = (myHat % 2 === 1 ? | |
[1, 'odd', 'up'] : | |
[-1, 'even', 'down'] | |
); | |
const middleHatIndex = rotation.indexOf(middleHat); | |
const simulatedFirstHat = rotation[ | |
(rotation.length + middleHatIndex + offset) % rotation.length | |
]; | |
log( | |
` My hat is ${myHatParity}, so the first prisoner should have gone ${direction} from ` + | |
`${middleHat} to get ${simulatedFirstHat}` | |
); | |
if (simulatedFirstHat === responses[0]) { | |
log(' And that\'s what happened, so this scenario is consistent.'); | |
return true; | |
} | |
log(' But that\'s not what happened, so this scenario is inconsistent.'); | |
return false; | |
}); | |
assert(scenariosConsistent.some(c => c)); | |
if (scenariosConsistent.every(c => c)) { | |
log(`Both scenarios work, so I'll arbitrarily pick ${scenarios[0].myHat}`); | |
return scenarios[0].myHat; | |
} | |
const correctScenario = (scenariosConsistent[0] ? scenarios[0] : scenarios[1]); | |
log(`So the only valid possibility is ${correctScenario.myHat}`); | |
return correctScenario.myHat; | |
} | |
assert(false); | |
return -1; | |
}; | |
const perms = (arr) => (arr.length === 0 ? [[]] : | |
[].concat(...range(arr.length).map( | |
i => perms(arr.filter(x => x !== arr[i])).map(perm => [arr[i], ...perm]) | |
) | |
)); | |
assert((() => { | |
const visualPerm = (arr) => perms(arr).map(perm => perm.join('')).join(','); | |
assert(visualPerm([]) === ''); | |
assert(visualPerm(['a', 'b']) === 'ab,ba'); | |
assert(visualPerm(['a', 'b', 'c']) === 'abc,acb,bac,bca,cab,cba'); | |
return true; | |
})()); | |
const simulate4 = (hats, log) => { | |
assert(hats.length === 4); | |
const names = ['A', 'B', 'C']; | |
const prisoners = range(3).map(i => ({ name: names[i], hat: hats[i] })); | |
const responses = []; | |
log( | |
`Prisoners ${names.join('')} have hats ${hats.slice(0, 3).join('')}, ` + | |
`(hat ${hats[3]} was omitted)\n\n` | |
); | |
let correctCount = 0; | |
range(3).forEach(i => { | |
const visibleHats = hats.slice(i + 1, 3); | |
log( | |
`Prisoner ${prisoners[i].name} is wearing hat ${hats[i]}, has heard ${responses.join('')}, ` + | |
`and can see ${visibleHats}` | |
); | |
const response = strategy(responses, visibleHats, str => log(` ${str}`)); | |
const correct = (response === hats[i]); | |
if (correct) { | |
correctCount++; | |
} | |
log(`The response was ${response}. This prisoner ${correct ? 'lives' : 'dies'}.\n\n`); | |
responses.push(response); | |
}); | |
log(`${correctCount} prisoner(s) lived.`); | |
return correctCount; | |
}; | |
const results = perms(range(4)).map(hats => { | |
let logOutput = ''; | |
const correctCount = simulate4(hats, str => { logOutput += `${str}\n`; }); | |
return { | |
hats, | |
logOutput, | |
correctCount, | |
}; | |
}); | |
console.log('minimum survival:', results.map(r => r.correctCount).reduce((x, y) => Math.min(x, y))); | |
console.log('mean survival:', | |
results.map(x => x.correctCount).reduce((x, y) => x + y) / results.length | |
); | |
console.log('results', results); | |
console.log('\n\n', results.map(r => r.logOutput).join('\n\n\n\n\n')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: