Skip to content

Instantly share code, notes, and snippets.

@voltrevo
Created March 12, 2016 09:36
Show Gist options
  • Save voltrevo/27e48c2ba3e17fab7982 to your computer and use it in GitHub Desktop.
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
'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'));
@voltrevo
Copy link
Author

Output:

minimum survival: 2
index.js:191 mean survival: 2.5
index.js:195 results [Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, Object]0: Object1: Object2: Object3: Object4: Object5: Object6: Object7: Object8: Object9: Object10: Object11: Object12: Object13: Object14: Object15: Object16: Object17: Object18: Object19: Object20: Object21: Object22: Object23: Objectlength: 24__proto__: Array[0]
index.js:197 

 Prisoners ABC have hats 012, (hat 3 was omitted)


Prisoner A is wearing hat 0, has heard , and can see 1,2
  possibilities: 0, 3
  I'm the first prisoner
  rotation is: 0, 1, 3
  lastHat(2) is even, so I will guess *down* from nextHat(1) to get 0
The response was 0. This prisoner lives.


Prisoner B is wearing hat 1, has heard 0, and can see 2
  possibilities: 1, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 0 to get 1
The response was 1. This prisoner lives.


Prisoner C is wearing hat 2, has heard 01, and can see 
  possibilities: 2, 3
  I'm the middle prisoner
  My possibilties are 2, 3
  That means there are two scenarios: [{"myHat":2,"rotation":[0,1,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 1 to get 0
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 1 to get 2
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 2
The response was 2. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 013, (hat 2 was omitted)


Prisoner A is wearing hat 0, has heard , and can see 1,3
  possibilities: 0, 2
  I'm the first prisoner
  rotation is: 0, 1, 2
  lastHat(3) is odd, so I will guess *up* from nextHat(1) to get 2
The response was 2. This prisoner dies.


Prisoner B is wearing hat 1, has heard 2, and can see 3
  possibilities: 0, 1
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 2
  Since lastHat is odd, I should go *down* from the first prisoner's guess 2 to get 1
The response was 1. This prisoner lives.


Prisoner C is wearing hat 3, has heard 21, and can see 
  possibilities: 0, 3
  I'm the middle prisoner
  My possibilties are 0, 3
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 1 to get 3
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 1 to get 2
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 3
The response was 3. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 021, (hat 3 was omitted)


Prisoner A is wearing hat 0, has heard , and can see 2,1
  possibilities: 0, 3
  I'm the first prisoner
  rotation is: 0, 2, 3
  lastHat(1) is odd, so I will guess *up* from nextHat(2) to get 3
The response was 3. This prisoner dies.


Prisoner B is wearing hat 2, has heard 3, and can see 1
  possibilities: 0, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 2, 3
  Since lastHat is odd, I should go *down* from the first prisoner's guess 3 to get 2
The response was 2. This prisoner lives.


Prisoner C is wearing hat 1, has heard 32, and can see 
  possibilities: 0, 1
  I'm the middle prisoner
  My possibilties are 0, 1
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":1,"rotation":[0,2,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 2 to get 1
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 2 to get 3
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 1
The response was 1. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 023, (hat 1 was omitted)


Prisoner A is wearing hat 0, has heard , and can see 2,3
  possibilities: 0, 1
  I'm the first prisoner
  rotation is: 0, 1, 2
  lastHat(3) is odd, so I will guess *up* from nextHat(2) to get 0
The response was 0. This prisoner lives.


Prisoner B is wearing hat 2, has heard 0, and can see 3
  possibilities: 1, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 2
  Since lastHat is odd, I should go *down* from the first prisoner's guess 0 to get 2
The response was 2. This prisoner lives.


Prisoner C is wearing hat 3, has heard 02, and can see 
  possibilities: 1, 3
  I'm the middle prisoner
  My possibilties are 1, 3
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 2 to get 3
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 2 to get 0
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 3
The response was 3. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 031, (hat 2 was omitted)


Prisoner A is wearing hat 0, has heard , and can see 3,1
  possibilities: 0, 2
  I'm the first prisoner
  rotation is: 0, 2, 3
  lastHat(1) is odd, so I will guess *up* from nextHat(3) to get 0
The response was 0. This prisoner lives.


Prisoner B is wearing hat 3, has heard 0, and can see 1
  possibilities: 2, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 2, 3
  Since lastHat is odd, I should go *down* from the first prisoner's guess 0 to get 3
The response was 3. This prisoner lives.


Prisoner C is wearing hat 1, has heard 03, and can see 
  possibilities: 1, 2
  I'm the middle prisoner
  My possibilties are 1, 2
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 3 to get 0
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 3 to get 1
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 1
The response was 1. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 032, (hat 1 was omitted)


Prisoner A is wearing hat 0, has heard , and can see 3,2
  possibilities: 0, 1
  I'm the first prisoner
  rotation is: 0, 1, 3
  lastHat(2) is even, so I will guess *down* from nextHat(3) to get 1
The response was 1. This prisoner dies.


Prisoner B is wearing hat 3, has heard 1, and can see 2
  possibilities: 0, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 1 to get 3
The response was 3. This prisoner lives.


Prisoner C is wearing hat 2, has heard 13, and can see 
  possibilities: 0, 2
  I'm the middle prisoner
  My possibilties are 0, 2
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 3 to get 2
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 3 to get 1
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 2
The response was 2. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 102, (hat 3 was omitted)


Prisoner A is wearing hat 1, has heard , and can see 0,2
  possibilities: 1, 3
  I'm the first prisoner
  rotation is: 0, 1, 3
  lastHat(2) is even, so I will guess *down* from nextHat(0) to get 3
The response was 3. This prisoner dies.


Prisoner B is wearing hat 0, has heard 3, and can see 2
  possibilities: 0, 1
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 3 to get 0
The response was 0. This prisoner lives.


Prisoner C is wearing hat 2, has heard 30, and can see 
  possibilities: 1, 2
  I'm the middle prisoner
  My possibilties are 1, 2
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 0 to get 2
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 0 to get 3
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 2
The response was 2. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 103, (hat 2 was omitted)


Prisoner A is wearing hat 1, has heard , and can see 0,3
  possibilities: 1, 2
  I'm the first prisoner
  rotation is: 0, 1, 2
  lastHat(3) is odd, so I will guess *up* from nextHat(0) to get 1
The response was 1. This prisoner lives.


Prisoner B is wearing hat 0, has heard 1, and can see 3
  possibilities: 0, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 2
  Since lastHat is odd, I should go *down* from the first prisoner's guess 1 to get 0
The response was 0. This prisoner lives.


Prisoner C is wearing hat 3, has heard 10, and can see 
  possibilities: 2, 3
  I'm the middle prisoner
  My possibilties are 2, 3
  That means there are two scenarios: [{"myHat":2,"rotation":[0,1,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 0 to get 3
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 0 to get 1
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 3
The response was 3. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 120, (hat 3 was omitted)


Prisoner A is wearing hat 1, has heard , and can see 2,0
  possibilities: 1, 3
  I'm the first prisoner
  rotation is: 1, 2, 3
  lastHat(0) is even, so I will guess *down* from nextHat(2) to get 1
The response was 1. This prisoner lives.


Prisoner B is wearing hat 2, has heard 1, and can see 0
  possibilities: 2, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 1, 2, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 1 to get 2
The response was 2. This prisoner lives.


Prisoner C is wearing hat 0, has heard 12, and can see 
  possibilities: 0, 3
  I'm the middle prisoner
  My possibilties are 0, 3
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 2 to get 1
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 2 to get 0
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 0
The response was 0. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 123, (hat 0 was omitted)


Prisoner A is wearing hat 1, has heard , and can see 2,3
  possibilities: 0, 1
  I'm the first prisoner
  rotation is: 0, 1, 2
  lastHat(3) is odd, so I will guess *up* from nextHat(2) to get 0
The response was 0. This prisoner dies.


Prisoner B is wearing hat 2, has heard 0, and can see 3
  possibilities: 1, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 2
  Since lastHat is odd, I should go *down* from the first prisoner's guess 0 to get 2
The response was 2. This prisoner lives.


Prisoner C is wearing hat 3, has heard 02, and can see 
  possibilities: 1, 3
  I'm the middle prisoner
  My possibilties are 1, 3
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 2 to get 3
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 2 to get 0
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 3
The response was 3. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 130, (hat 2 was omitted)


Prisoner A is wearing hat 1, has heard , and can see 3,0
  possibilities: 1, 2
  I'm the first prisoner
  rotation is: 1, 2, 3
  lastHat(0) is even, so I will guess *down* from nextHat(3) to get 2
The response was 2. This prisoner dies.


Prisoner B is wearing hat 3, has heard 2, and can see 0
  possibilities: 1, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 1, 2, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 2 to get 3
The response was 3. This prisoner lives.


Prisoner C is wearing hat 0, has heard 23, and can see 
  possibilities: 0, 1
  I'm the middle prisoner
  My possibilties are 0, 1
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":1,"rotation":[0,2,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 3 to get 2
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 3 to get 0
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 0
The response was 0. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 132, (hat 0 was omitted)


Prisoner A is wearing hat 1, has heard , and can see 3,2
  possibilities: 0, 1
  I'm the first prisoner
  rotation is: 0, 1, 3
  lastHat(2) is even, so I will guess *down* from nextHat(3) to get 1
The response was 1. This prisoner lives.


Prisoner B is wearing hat 3, has heard 1, and can see 2
  possibilities: 0, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 1 to get 3
The response was 3. This prisoner lives.


Prisoner C is wearing hat 2, has heard 13, and can see 
  possibilities: 0, 2
  I'm the middle prisoner
  My possibilties are 0, 2
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 3 to get 2
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 3 to get 1
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 2
The response was 2. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 201, (hat 3 was omitted)


Prisoner A is wearing hat 2, has heard , and can see 0,1
  possibilities: 2, 3
  I'm the first prisoner
  rotation is: 0, 2, 3
  lastHat(1) is odd, so I will guess *up* from nextHat(0) to get 2
The response was 2. This prisoner lives.


Prisoner B is wearing hat 0, has heard 2, and can see 1
  possibilities: 0, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 2, 3
  Since lastHat is odd, I should go *down* from the first prisoner's guess 2 to get 0
The response was 0. This prisoner lives.


Prisoner C is wearing hat 1, has heard 20, and can see 
  possibilities: 1, 3
  I'm the middle prisoner
  My possibilties are 1, 3
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 0 to get 2
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 0 to get 1
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 1
The response was 1. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 203, (hat 1 was omitted)


Prisoner A is wearing hat 2, has heard , and can see 0,3
  possibilities: 1, 2
  I'm the first prisoner
  rotation is: 0, 1, 2
  lastHat(3) is odd, so I will guess *up* from nextHat(0) to get 1
The response was 1. This prisoner dies.


Prisoner B is wearing hat 0, has heard 1, and can see 3
  possibilities: 0, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 2
  Since lastHat is odd, I should go *down* from the first prisoner's guess 1 to get 0
The response was 0. This prisoner lives.


Prisoner C is wearing hat 3, has heard 10, and can see 
  possibilities: 2, 3
  I'm the middle prisoner
  My possibilties are 2, 3
  That means there are two scenarios: [{"myHat":2,"rotation":[0,1,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 0 to get 3
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 0 to get 1
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 3
The response was 3. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 210, (hat 3 was omitted)


Prisoner A is wearing hat 2, has heard , and can see 1,0
  possibilities: 2, 3
  I'm the first prisoner
  rotation is: 1, 2, 3
  lastHat(0) is even, so I will guess *down* from nextHat(1) to get 3
The response was 3. This prisoner dies.


Prisoner B is wearing hat 1, has heard 3, and can see 0
  possibilities: 1, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 1, 2, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 3 to get 1
The response was 1. This prisoner lives.


Prisoner C is wearing hat 0, has heard 31, and can see 
  possibilities: 0, 2
  I'm the middle prisoner
  My possibilties are 0, 2
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 1 to get 3
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 1 to get 0
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 0
The response was 0. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 213, (hat 0 was omitted)


Prisoner A is wearing hat 2, has heard , and can see 1,3
  possibilities: 0, 2
  I'm the first prisoner
  rotation is: 0, 1, 2
  lastHat(3) is odd, so I will guess *up* from nextHat(1) to get 2
The response was 2. This prisoner lives.


Prisoner B is wearing hat 1, has heard 2, and can see 3
  possibilities: 0, 1
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 2
  Since lastHat is odd, I should go *down* from the first prisoner's guess 2 to get 1
The response was 1. This prisoner lives.


Prisoner C is wearing hat 3, has heard 21, and can see 
  possibilities: 0, 3
  I'm the middle prisoner
  My possibilties are 0, 3
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 1 to get 3
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 1 to get 2
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 3
The response was 3. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 230, (hat 1 was omitted)


Prisoner A is wearing hat 2, has heard , and can see 3,0
  possibilities: 1, 2
  I'm the first prisoner
  rotation is: 1, 2, 3
  lastHat(0) is even, so I will guess *down* from nextHat(3) to get 2
The response was 2. This prisoner lives.


Prisoner B is wearing hat 3, has heard 2, and can see 0
  possibilities: 1, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 1, 2, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 2 to get 3
The response was 3. This prisoner lives.


Prisoner C is wearing hat 0, has heard 23, and can see 
  possibilities: 0, 1
  I'm the middle prisoner
  My possibilties are 0, 1
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":1,"rotation":[0,2,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 3 to get 2
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 3 to get 0
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 0
The response was 0. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 231, (hat 0 was omitted)


Prisoner A is wearing hat 2, has heard , and can see 3,1
  possibilities: 0, 2
  I'm the first prisoner
  rotation is: 0, 2, 3
  lastHat(1) is odd, so I will guess *up* from nextHat(3) to get 0
The response was 0. This prisoner dies.


Prisoner B is wearing hat 3, has heard 0, and can see 1
  possibilities: 2, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 2, 3
  Since lastHat is odd, I should go *down* from the first prisoner's guess 0 to get 3
The response was 3. This prisoner lives.


Prisoner C is wearing hat 1, has heard 03, and can see 
  possibilities: 1, 2
  I'm the middle prisoner
  My possibilties are 1, 2
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 3 to get 0
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 3 to get 1
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 1
The response was 1. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 301, (hat 2 was omitted)


Prisoner A is wearing hat 3, has heard , and can see 0,1
  possibilities: 2, 3
  I'm the first prisoner
  rotation is: 0, 2, 3
  lastHat(1) is odd, so I will guess *up* from nextHat(0) to get 2
The response was 2. This prisoner dies.


Prisoner B is wearing hat 0, has heard 2, and can see 1
  possibilities: 0, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 2, 3
  Since lastHat is odd, I should go *down* from the first prisoner's guess 2 to get 0
The response was 0. This prisoner lives.


Prisoner C is wearing hat 1, has heard 20, and can see 
  possibilities: 1, 3
  I'm the middle prisoner
  My possibilties are 1, 3
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 0 to get 2
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 0 to get 1
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 1
The response was 1. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 302, (hat 1 was omitted)


Prisoner A is wearing hat 3, has heard , and can see 0,2
  possibilities: 1, 3
  I'm the first prisoner
  rotation is: 0, 1, 3
  lastHat(2) is even, so I will guess *down* from nextHat(0) to get 3
The response was 3. This prisoner lives.


Prisoner B is wearing hat 0, has heard 3, and can see 2
  possibilities: 0, 1
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 3 to get 0
The response was 0. This prisoner lives.


Prisoner C is wearing hat 2, has heard 30, and can see 
  possibilities: 1, 2
  I'm the middle prisoner
  My possibilties are 1, 2
  That means there are two scenarios: [{"myHat":1,"rotation":[0,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 0 to get 2
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 0 to get 3
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 2
The response was 2. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 310, (hat 2 was omitted)


Prisoner A is wearing hat 3, has heard , and can see 1,0
  possibilities: 2, 3
  I'm the first prisoner
  rotation is: 1, 2, 3
  lastHat(0) is even, so I will guess *down* from nextHat(1) to get 3
The response was 3. This prisoner lives.


Prisoner B is wearing hat 1, has heard 3, and can see 0
  possibilities: 1, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 1, 2, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 3 to get 1
The response was 1. This prisoner lives.


Prisoner C is wearing hat 0, has heard 31, and can see 
  possibilities: 0, 2
  I'm the middle prisoner
  My possibilties are 0, 2
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":2,"rotation":[0,1,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 1 to get 3
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 1 to get 0
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 0
The response was 0. This prisoner lives.


3 prisoner(s) lived.





Prisoners ABC have hats 312, (hat 0 was omitted)


Prisoner A is wearing hat 3, has heard , and can see 1,2
  possibilities: 0, 3
  I'm the first prisoner
  rotation is: 0, 1, 3
  lastHat(2) is even, so I will guess *down* from nextHat(1) to get 0
The response was 0. This prisoner dies.


Prisoner B is wearing hat 1, has heard 0, and can see 2
  possibilities: 1, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 1, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 0 to get 1
The response was 1. This prisoner lives.


Prisoner C is wearing hat 2, has heard 01, and can see 
  possibilities: 2, 3
  I'm the middle prisoner
  My possibilties are 2, 3
  That means there are two scenarios: [{"myHat":2,"rotation":[0,1,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 2:
    My hat is even, so the first prisoner should have gone down from 1 to get 0
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 1 to get 2
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 2
The response was 2. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 320, (hat 1 was omitted)


Prisoner A is wearing hat 3, has heard , and can see 2,0
  possibilities: 1, 3
  I'm the first prisoner
  rotation is: 1, 2, 3
  lastHat(0) is even, so I will guess *down* from nextHat(2) to get 1
The response was 1. This prisoner dies.


Prisoner B is wearing hat 2, has heard 1, and can see 0
  possibilities: 2, 3
  I'm the middle prisoner
  The first prisoner's rotation should have been: 1, 2, 3
  Since lastHat is even, I should go *up* from the first prisoner's guess 1 to get 2
The response was 2. This prisoner lives.


Prisoner C is wearing hat 0, has heard 12, and can see 
  possibilities: 0, 3
  I'm the middle prisoner
  My possibilties are 0, 3
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":3,"rotation":[0,1,2]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 2 to get 1
    And that's what happened, so this scenario is consistent.
  Scenario with my hat being 3:
    My hat is odd, so the first prisoner should have gone up from 2 to get 0
    But that's not what happened, so this scenario is inconsistent.
  So the only valid possibility is 0
The response was 0. This prisoner lives.


2 prisoner(s) lived.





Prisoners ABC have hats 321, (hat 0 was omitted)


Prisoner A is wearing hat 3, has heard , and can see 2,1
  possibilities: 0, 3
  I'm the first prisoner
  rotation is: 0, 2, 3
  lastHat(1) is odd, so I will guess *up* from nextHat(2) to get 3
The response was 3. This prisoner lives.


Prisoner B is wearing hat 2, has heard 3, and can see 1
  possibilities: 0, 2
  I'm the middle prisoner
  The first prisoner's rotation should have been: 0, 2, 3
  Since lastHat is odd, I should go *down* from the first prisoner's guess 3 to get 2
The response was 2. This prisoner lives.


Prisoner C is wearing hat 1, has heard 32, and can see 
  possibilities: 0, 1
  I'm the middle prisoner
  My possibilties are 0, 1
  That means there are two scenarios: [{"myHat":0,"rotation":[1,2,3]},{"myHat":1,"rotation":[0,2,3]}]
  Scenario with my hat being 0:
    My hat is even, so the first prisoner should have gone down from 2 to get 1
    But that's not what happened, so this scenario is inconsistent.
  Scenario with my hat being 1:
    My hat is odd, so the first prisoner should have gone up from 2 to get 3
    And that's what happened, so this scenario is consistent.
  So the only valid possibility is 1
The response was 1. This prisoner lives.


3 prisoner(s) lived.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment