puzzle #10

Wide tables, equations, and code scroll sideways. Swipe, or Tab to focus them and use the left and right arrow keys.

100 prisoners and hats

You will learn: Use a parity message to distinguish a worst-case guarantee from a fair-hat probability.

Start with: Shannon entropy

One hundred people stand in a line wearing black or white hats. Each sees the hats ahead, but neither their own nor those behind. They speak from the back toward the front, each saying exactly one color as their guess. Everyone hears every earlier answer. They may agree on a strategy beforehand, but cannot gesture, change the timing to signal information, or use extra words.

At first it seems that everyone must guess. In fact, one person can use their answer to communicate a single bit that makes the remaining ninety-nine answers certain. The useful message is not an inventory of the hats. It is the parity of a count.

Inspect one person’s information

Numbering follows speaking order: person 1 stands at the back; person 100 is at the front. Black = 1; white = 0. Each person sees only higher-numbered hats.

What person 2 can know

Visible black hats: 56 of 98 ahead. Earlier words heard: 1. Their own hat is hidden from them.

Inspect visible hats and earlier words in order

Visible: White (0), White (0), Black (1), Black (1), White (0), Black (1), Black (1), Black (1), Black (1), Black (1), Black (1), White (0), Black (1), Black (1), White (0), White (0), White (0), Black (1), Black (1), White (0), White (0), White (0), White (0), Black (1), White (0), Black (1), White (0), Black (1), Black (1), Black (1), White (0), White (0), White (0), Black (1), Black (1), Black (1), Black (1), White (0), White (0), Black (1), Black (1), Black (1), White (0), Black (1), White (0), Black (1), Black (1), Black (1), Black (1), Black (1), White (0), Black (1), White (0), White (0), Black (1), White (0), Black (1), White (0), Black (1), White (0), Black (1), White (0), White (0), Black (1), Black (1), Black (1), White (0), Black (1), White (0), White (0), White (0), Black (1), White (0), White (0), White (0), Black (1), White (0), White (0), Black (1), Black (1), Black (1), Black (1), Black (1), White (0), White (0), Black (1), White (0), Black (1), Black (1), Black (1), Black (1), White (0), Black (1), Black (1), Black (1), White (0), Black (1), Black (1).

Heard: White (0).

Decode the first parity word, removing the colors already established by speakers 2 onward and those still visible ahead.

QuantityResult
Guaranteed correct, any assignment99
Expected correct, independent fair hats99.5
Probability everyone is correct, fair hats1/2
A pre-agreed parity code; one color word per person, heard by everyone, from back to front. No gestures or extra communication. The guarantee does not require random hats; the displayed probability statements do.

Black means one and white means zero. Number people in speaking order, so person one is at the back. Their word is black if they see an odd number of black hats and white if they see an even number. This is a statement about everyone ahead, not a claim based on seeing their own hat.

Select a later speaker and inspect their visible hats and heard words before revealing the actual assignment. The full grid is an omniscient view for checking the calculation; those actual hats are not extra information available to the speaker.

Why the second person knows

Suppose the first word is black: the total number of black hats among people two through one hundred is odd. Person two counts the black hats ahead. If that count is even, their own hat must be black; if it is odd, their own hat must be white. Their spoken answer is therefore correct, and everyone later can treat it as a known hat color.

Person three removes person two’s established color from the announced parity, then compares the remaining parity with the hats they can see. The same argument continues by induction. At every turn after the first, exactly one unknown hat remains in the parity equation.

In bit notation, the first answer is the exclusive-or of hats two onward. A later answer is that first bit, exclusive-or all established answers from person two up to the previous speaker, exclusive-or all visible hats ahead. Applying exclusive-or twice to the same bit cancels it. Only the current person’s bit survives.

For a four-person example with hats black, white, black, white, the visible black count for person one is one. They say black. Person two sees one black, so says white; person three sees zero black after removing person two’s white, so says black. Person four removes the already established black and says white. Here all four happen to be correct. Flipping only the first hat changes no spoken word and makes only that first answer wrong.

A guarantee and a probability are different claims

Every assignment gives at least ninety-nine correct answers. No randomness assumption is needed for this guarantee. If hats are independently fair, the first person’s own hat is independent of the parity they announce, so their probability of being correct is one half. The total correct count is then either ninety-nine or one hundred, with equal probability; its expectation is 99.5.

It would be wrong to multiply one hundred independent guessing probabilities. The later answers are certain under the agreed protocol. Conversely, no permitted strategy can guarantee all one hundred: the first speaker receives identical information in two assignments differing only in their own hat, so must give the same answer to both.

Make a prediction

With only one person in the line, what remains of the guarantee?

Explore the answer

The guaranteed number correct is zero. The empty visible set has even parity, so this convention says white. Under a fair random hat, that answer is correct half the time. The probability statement remains meaningful even though no later person benefits from the code.

The Carnegie Mellon puzzle solution, problem one, describes this line-and-parity protocol. Changing who can see whom or allowing more colors creates a different information problem. Continue to entropy to connect the one-bit message with information, or Kruskal count for a different mechanism by which initially separate paths become predictable.

Reset all settings