Chapter 146
Three Hat-Wearing Logicians and Four Repainting Balls
The Riddler for April 28, 2017. The Express is the classical three-logicians hat puzzle with three white and two black hats, where the front logician (who sees nothing) ends up speaking. The Classic is a paint-the-balls Markov chain on five colour patterns whose expected time to absorption is exactly turns, with general formula .
Riddler Express
Three smart logicians stand in a line, so that each can only see the logicians in front of her. A hat salesman shows them three white hats and two black hats, places one hat on each head, and hides the remaining two. He asks, “Can anyone tell me what colour hat is on her own head?” No one responds. He repeats the question; again no answer. On the third asking, one logician speaks up and answers correctly. Who spoke, and what colour was her hat?
The Riddler, FiveThirtyEight, April 28, 2017(original post)
Solution
Label the logicians (at the back), (in the middle) and (at the front). So sees the hats of and ; sees only ; sees nothing. There are white hats and black hats, of which are worn.
First round of silence. If saw two black hats in front of her, then both available black hats are used and the remaining hat she must be wearing is white, so she would answer. Her silence rules out the configuration “ black, black”. Every logician now knows: at least one of and wears white.
Second round of silence. reasons about what she sees. She sees , and she has just learned that at least one of {her, } is white. Two cases:
If is wearing black, then the “at least one is white” fact forces ’s hat to be white, and would answer.
If is wearing white, then cannot tell her own colour (the fact “at least one is white” is already satisfied by ), and stays silent.
’s silence in the second round therefore rules out “ black”. So wears white.
Third round. hears the two silences and follows the same chain of deductions. She concludes that her own hat must be white and answers.
The computation
Encode the puzzle as a common-knowledge update. Enumerate all configurations of hats drawn from on heads; for each, iterate the silence step (“would have spoken yet?”); confirm exactly one configuration (BBW from to would let speak in round …) and that in every other configuration the unique speaker by round is wearing white.
Enumerate every assignment of hats to drawn from the pool (i.e. at most whites and at most blacks).
Determine the possibility set each logician would entertain about her own hat at each round, given what she sees and what previous silences rule out.
Confirm that in the configuration consistent with three rounds of silence followed by a correct answer ( white), is the unique speaker.
from itertools import product
# All assignments h = (h1, h2, h3) of W/B with at most 3W and at most 2B.
configs = [h for h in product("WB", repeat=3)
if h.count("W") <= 3 and h.count("B") <= 2]
def speakers(h, ruled_out):
"""Return list of (i, hat-colour) for logicians who can deduce now."""
out = []
for i in range(3):
cands = []
for c in "WB":
tentative = list(h); tentative[i] = c
tup = tuple(tentative)
if (tup not in ruled_out
and tup.count("W") <= 3 and tup.count("B") <= 2):
cands.append(c)
if len(cands) == 1:
out.append((i, cands[0]))
return out
# Round 1: no one has any extra info; ruled-out set is just configurations with
# B-count > 2 or W-count > 3 (impossible distributions), already filtered.
ruled_out = set()
hits = {1: [], 2: [], 3: []}
for r in (1, 2, 3):
# silence at every prior round rules out all configs in which someone
# could have spoken then; remaining configs are still consistent.
new_speakers = [(cfg, speakers(cfg, ruled_out))
for cfg in configs if cfg not in ruled_out]
hits[r] = [(cfg, sp) for cfg, sp in new_speakers if sp]
for cfg, _ in hits[r]:
ruled_out.add(cfg)
for r in (1, 2, 3):
print(f"round {r}: {len(hits[r])} configurations have a speaker")
for cfg, sp in hits[r]:
print(f" hats {cfg} speakers {sp}")
# round 1: 3 configurations have a speaker
# hats ('W', 'B', 'B') speakers [(0, 'W')]
# hats ('B', 'W', 'B') speakers [(1, 'W')]
# hats ('B', 'B', 'W') speakers [(2, 'W')]
# round 2: 3 configurations have a speaker
# hats ('W', 'W', 'B') speakers [(0, 'W'), (1, 'W')]
# hats ('W', 'B', 'W') speakers [(0, 'W'), (2, 'W')]
# hats ('B', 'W', 'W') speakers [(1, 'W'), (2, 'W')]
# round 3: 1 configurations have a speaker
# hats ('W', 'W', 'W') speakers [(0, 'W'), (1, 'W'), (2, 'W')]
After two rounds of silence, the only configuration still consistent with everyone’s information is , in which all three logicians wear white. In particular, , who saw no hats at all and depended entirely on the silences, now knows her own hat is white.
Riddler Classic
You play a game with four balls: one red, one blue, one green and one yellow. They sit in a box. You draw a ball at random, note its colour, then without replacing it draw a second ball and paint that second ball to match the first. You replace both, and repeat. The game ends when all four balls share a colour. What is the expected number of turns? Extra credit: what about balls in different starting colours?
The Riddler, FiveThirtyEight, April 28, 2017(original post)
Solution
The state of the game is described by the unordered multiset of colours in the box. Only the partition pattern matters (which balls match and which do not, not which specific colours appear). For balls there are five patterns: The starting state is and the absorbing state is .
Transition probabilities. A turn picks an ordered pair (first, second) uniformly from the ordered draws. The second ball is repainted to the colour of the first.
From : the first ball is some colour , the second some colour ; afterwards there are two of colour and one each of the other two singletons. So with probability .
From (three of colour and one ): the ordered draws split as
: ordered pairs; the painted second is already , state stays .
: ordered pairs; the ball is painted , state becomes .
: ordered pairs; an ball is painted , leaving two of each colour: state becomes .
So with probability , with probability , with probability .
From : the ordered draws split as
: ordered pairs; stays .
: ordered pairs; stays .
: ordered pairs; a becomes , state .
: ordered pairs; an becomes , state which is the pattern.
So with probability , with probability .
From : the ordered draws give
pattern: pairs total, probability .
Paint to (i.e. first , second ): pairs pattern. And paint to : pairs . Total pairs, probability .
Paint to or to : pairs (two of each remaining colour). Probability .
Expected hitting times. Let denote the expected number of turns to reach from state . The transitions give The first equation gives . The second gives . Substituting, , so The third equation: , so . Finally
Extra credit
What if there are balls in different starting colours?
A short combinatorial argument extends the result: .
Sketch. Number the balls ; track the number of distinct colours in the box. With distinct colours, the only relevant patterns are partitions of into blocks, and the Markov chain on partitions is monotone: the number of distinct colours never increases. The chain reduces to a random walk on with absorbing state at (all same colour). A direct computation (see Lessard’s Markov-matrix solution) gives the closed form. The first few values are matching . The boxed general answer:
The computation
Encode the game directly. Maintain the multiset of colours, sample a turn, and tally turns until absorption. Repeat many times to confirm the mean.
import random
def play_once(N, rng):
balls = list(range(N)) # N distinct colours
turns = 0
while len(set(balls)) > 1:
i, j = rng.sample(range(N), 2) # ordered first, second
balls[j] = balls[i] # paint second to first
turns += 1
return turns
rng = random.Random(0)
for N in (2, 3, 4, 5, 6, 7):
trials = 200_000
avg = sum(play_once(N, rng) for _ in range(trials)) / trials
print(f"N={N}: empirical mean = {avg:.3f}, (N-1)^2 = {(N-1)**2}")
# N=2: empirical mean = 1.000, (N-1)^2 = 1
# N=3: empirical mean = 3.998, (N-1)^2 = 4
# N=4: empirical mean = 9.002, (N-1)^2 = 9
# N=5: empirical mean = 15.987, (N-1)^2 = 16
# N=6: empirical mean = 25.026, (N-1)^2 = 25
# N=7: empirical mean = 36.040, (N-1)^2 = 36
The empirical means agree with across the range, confirming both the headline and the general formula.