The spirit
Every round, the current guesser needs to decide between two potential candidate-answers for themselves: a + b
or |a - b|
. If only one of the candidates is valid (i.e, positive integer), then the player can answer immediately with the only remaining candidate.
Otherwise, the current guesser must recall all prior rounds, and simulate the conclusions drawn by the previous players on their turns (including themselves from 3 rounds ago!!!). This is akin to a tree search, where a leaf node is pruned if a prior round would have deemed that candidate impossible. In turn, the player would then deduce that that entire half of the search tree is invalid, and must conclude that the other candidate is the valid one. If no leaf-pruning is possible across all rounds, then the player cannot determine their number and must pass.
The code
The code assumes we are on round 6 for person C.
Exhaustive search of candidate (aA,bB,cC) pairs are passed to a validator solve
function, which returns all potential valid hats assuming n
rounds have elapsed, and all previous rounds have produced I don't know
answers.