Hats to leave you scratching your head

Logic; Difficulty: hard

Photo by Héctor J. Rivas from Unsplash

Yet another Prisoner–Warden–Hats conundrum

This is from this week’s Riddler: a particularly tough variant of the Prisoner-Warden class of problems.

Four people are trying to escape from a room. Guards have placed a hat on each person’s head, and each hat is one of three colors: red, yellow or blue. The four people are arranged at the vertices of a square, with an obstacle in the middle. Each person can see the hats on the heads of those on adjacent vertices of the square, but they cannot see the hat of the person diagonally across from them. They also do not know the color of the hat on their own head.

Each person must guess the color of the hat on their own head. If at least one person guesses correctly, they can all escape the room together. No communication is allowed once the hats are placed on their heads, but they can coordinate on a strategy beforehand. They also know how they will be arranged in the square.

How can they be guaranteed to escape the room?

Understanding the set up better

These should be obvious but it’s worth reiterating the constraints we’re working with here:

  • The prisoners have to provide their answer silently to the warden i.e. a prisoner cannot know any of the others’ guesses
  • The warden is free to place a hat of any color on the prisoners’ heads. i.e. a hat color can be used any number of times needed

Let A,B,C,D be the prisoners arranged as shown in the figure below with hats a,b,c,d placed on their heads respectively. Each of a,b,c,d can be R, Y or B.  Given the central obstruction, it should be clear that A and C essentially have the same information: the colors b and d visible. And similarly for B and D who can both see colors a,c. There are 3 possible hat choices for each of the 4 prisoners resulting in 81 permutations of hats for our four prisoners. So if there is a strategy that allows them to escape regardless of which one of these 81 arrangements the warden chooses, that strategy has to be predicated on there being a mapping p-q that A and C pick as their guess for every possible b-d that they observe (9 choices), and similarly, a combination r-s that B and D will pick based on the a-c hat combination they see (again 9 combinations), such that in each of the possible 81 combinations the warden places on the prisoners’ heads, this mapping will result in at least one prisoner getting their own color right.

The question then is, does such a mapping exist and if it does, how do we find it?

hats square

Brute force it?

 Now you can have 9! = 362880 possible ways to map bd hat color combinations into pq guesses for A and C. And similarly the same number for B and D leaving us with over a 131 billion combinations to explore and potentially identify a winning strategy from that guarantees an escape.  So you can get your computer out and have it pore through the options—but then where’s the fun in that? Also it’s reasonable to assume the prisoners aren’t going to have access to a computer.

General Intuition

The only way the prisoners are going to escape is if there is some way for them to transfer information about their guesses to others. They cannot communicate once they have hats on their heads but they can agree to a strategy beforehand. Our intuition is that A and C guess using functions \(f\) and \(g\) based on the colors \(b,d\) they see. But no matter what A and C guess, they alone cannot be expected to get at least one of their colors right in all possible cases. So then B and D, use functions \(h\) and \(i\) that depend on \(a,c \) and also (this is the key) \(f\) and \(g\) to cover the cases when A’s and C’s guesses both don’t work out. So what we are after are guesses of the form:

\(A: f(b,d)\)
\(C: g(b,d)\)
\(B: h(a,c,f,g)\)
\(D: i(a,c,f,g)\)

Solution:

The puzzle is made easier once we use numbers for hat colors and invoke ideas from basic modular arithmetic. Let the digits 0,1,2, correspond to the colors R,Y,B respectively. Think of a clock with only 3 numbers on the dial instead of twelve: 0, 1 and 2. So when you get to 3, you are back on 0. Also when you subtract say 2 from 0, you go two steps anti-clockwise from 0 to arrive at 1. This is all you really need to know about modular arithmetic for our solution.

All operations described henceforth are going to be mod 3 i.e. use the dial below to determine the value when you go outside the range 0 to 2. Hereon I may omit specifying mod 3 to make reading and understanding the solution easier.

Now let’s start with the guesses A and C make: For a winning strategy, we intuit that these as mentioned earlier, cannot be entirely random but rather functions of the colors b and d that they see on B’s and D’s heads. Similarly B and D will make guesses based on the a and c, they see as well as knowing the functions A and C will be using. What could these functions be? Turns out that sums and differences work well here as pointed out by Sandi Sandman who helped me when I was stuck.

So let’s say that A and B guess: 

\[
f(b,d) = (b+d)  \\ \\
g(b,d) = (b-d) 
\]

This is just saying that A takes the numbers corresponding to B’s and D’s hats (remember R=0, Y=1, B=2), adds them up and if their sum happens to be greater than 2, uses the remainder on dividing by three i.e. picking. the correct number from our clock with three points (red text above). C, does the same with the difference of B’s and D’s colors.

As an example, let’s assume the colors on B D are 0 1  i.e.  Red, Yellow. This means A will guess \((0 + 1) mod 3 = 1 \equiv\) Yellow (Y) and C will guess \((0-1) mod 3  = 2 \equiv \)Blue (B).

Now A and C could themselves have any one of 9 combinations on their heads. Their guesses based on what they see on B and D (0,1) will set them all free in 5 of those 9 instances. These 5 instances in our example are: (1,0)  (1,1),  (1,2),  (0,2),  (2,2).  But there are 4 other instances where their guesses do the group no good. These are when A and C have hats (0,0),  (0,1),  (2,0),  (2,1).  But B and D still have their guesses available. We now have to figure out a way for them to use their guesses to cover the remaining situations such that they have at least one correct guess. 

Now remember that B and D can actually see the hat colors on A and C (but not on their own heads of course!). So they have some information on what they need to cover. Let’s assume they see (0,1) on A and C, an instance where A’s and C’s guesses previously discussed are wasted.

So here’s where we are at so far:

A and C see (0,1) on B and D and per agreed upon strategy guess (1,2). This guess would have gotten them free in 5 out of 9 possible combinations on their head but unfortunately not in this instance when they have (0,1). With me?

B and D have  to work on the worst case assumption that the hat colors they themselves have and the functions they know A and C will use for their guesses will result in both A AND C making incorrect guesses. At least one of them (B or D) will have to guess correctly in those four remaining possibilities.

It follows then that if B and D assume that A’s and C’s guesses were wrong, then A and C were either off by 1 place higher or 1 lower in their guess using \(b\) and \(d\) (since there are only 3 choices!):

\begin{align*}
(b+d) 
& \begin{cases}
& =(a+1) \qquad \textit{call this case } m_1 \\ 
& \quad OR \\ 
& =(a-1)  \qquad \textit{call this case } m_2 \\ 
\end{cases} \\ 
& \textit{since } (b+d)  \textit{ didn’t provide A with a correct guess}
\end{align*} 

Using the same reasoning:

\begin{align*}
(b-d) 
& \begin{cases}
&= (c+1)  \qquad \textit{call this case } n_1 \\ 
& \quad OR \\ 
&= (c-1) \qquad \textit{call this case } n_2  \\ 
\end{cases} \\ 
& \textit{since } (b-d)  \textit{ didn’t provide C with a correct guess}
\end{align*}

B and D now have some information on their own hats in the worst case situation. We’re getting closer to a workable strategy now! On seeing b+d and b – d if you’re now thinking of recovering b and d from an addition and subtraction operation, you’re on the right track! These are mod 3 operations though and so a little more complicated, but the usual intuition of getting \(b\) and \(d\) holds. 

Adding \((b+d)mod3 \) and \((b-d)mod3 \) gives us something only dependent on \(b\), eliminating \(d\) for all b and d! Let’s call it \(\hat b\). You can check that \(b\ = (2\hat b) \ mod3 \).

Similarly,  \( (2((b+d)mod3 – (b-d)mod3))mod3 \) gives us \(d\).

So now B and D can deduce that B’s hat \(b\) and D’s hat \(d\) are one of the following four combinations that cover the cases when A and C both get their guesses wrong:

\begin{align*}
& b = 2(m_1 + n_1) \quad and \qquad \color{blue}{ d = 2(m_1 – n_1)}  \quad OR \\
&\color{red}{ b = 2(m_1 + n_2)} \quad and \qquad  d = 2(m_1 – n_2) \quad OR \\
&\color{red}{ b =2( m_2 + n_1)} \quad and \qquad d = 2(m_2 – n_1) \quad OR \\
& b =2( m_2 + n_2) \quad and \qquad \color{blue}{ d = 2(m_2 – n_2)}  \\
\end{align*}

But these are four \(b,d\) combinations you say. How can we pick so that at least one of the \(b\) or \(d\) is a correct guess? The key is seeing the second and third above will give the same result for \(b\)  and the first and fourth give the same result for \(d\).  This is because by using \(m_1, \ m_2, \  n_1, \  n_2\)  the choices can be rewritten as: 

\begin{align*}
& b = 2((a+1) + (c+1)) \quad and \qquad \color{blue}{ d = 2((a+1) – (c +1))}  \quad OR \\
&\color{red}{ b = 2((a+1) + (c-1))} \quad and \qquad  d = 2((a+1) – (c-1)) \quad OR \\
&\color{red}{ b =2( (a-1) + (c+1))} \quad and \qquad d = 2((a-1) – (c+1)) \quad OR \\
& b =2( (a-1) + (c-1)) \quad and \qquad \color{blue}{ d = 2((a-1) – (c-1))}  \\
\end{align*}

(you can verify this — remember these are all mod 3 operations)

What this now implies is that if B and D pick the red and blue options respectively:

\begin{align*}
b &= 2(a+c)  \\
d &= 2(a-c)
\end{align*}

they are guaranteed to get one of their guesses correct. Remember this is only true when A’s and C’s guesses based on their strategy, both end up being wrong. But this is precisely the scenario we set out to cover since otherwise one or both of A and C will guess correctly and set the group free.

Let’s finish up our example where ACBD have on (0,1,0,1). A and C guess (1,2) – a lost cause! B and D per the above formula guess 2,1. D guesses correctly and they yelp for joy as they are set free by the warden who is true to his word.  

So there you have it! This solves the problem.

Summary: 

Here is the strategy of guesses  then:

\begin{align*} 
& \bbox[yellow, 10px]{A: (b+d) \ mod \ 3} \\
& \bbox[yellow, 10px]{C: (b-d) \ mod \ 3 }\\
& \bbox[yellow, 10px]{B: 2(a+c) \ mod \ 3} \\
& \bbox[yellow, 10px]{D: 2(a-c) \ mod \ 3 }
\end{align*}

This is all too easy to verify in a spreadsheet or with a few lines of code.

Prisoner Solution Cheat Sheet:

Here’s the prisoners’ cheat sheet listing all combinations and guesses (0 = R, 1 = Y, 2 = B):

B and D have onA and C guess A and C have onB and D guess
0,00,0 0,00,0
0,11,2 0,12,1
0,22,1 0,21,2
1,01,1 1,02,2
1,12,0 1,11,0
1,20,2 1,20,1
2,02,2 2,01,1
2,10,1 2,10,2
2,21,0 2,22,0

You can pick any one of the 81 combinations of hats they have on and the corresponding guesses from the table above, to verify they escape every time!

Closing Thoughts:

This is quite a hard problem overall — or at least was for me. Had me scratching my head for several hours. I doff my hat (of unknown color) to Eric Dallal, the creator this puzzle is attributed to, and Zach Wissner-Gross, who included it in this week’s Riddler. You might have a more elegant solution that makes the answer and a/the strategy more obvious. If you do, please share via the comments or an email using the contact form (link in page footer).

Edit: 

  1. Reader Nis pointed out a simplification to my original final guessing strategy equations which I have partially incorporated. You can see the simplification in their comment here.
  2. Reader PJ has used an interesting approach: search through possible linear combinations of visible hat colors and get coefficients that allow for a solution of all 81 cases. You can find their solution outlined in this comment below.
Like this content? Subscribe by email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

11 thoughts on “Hats to leave you scratching your head”

    1. Yeah it was hard and the solution here isn’t simple—I’m not happy with the it because it feels like I’m missing the intuition that makes for a more obvious understanding. On the other hand the solution looks more complicated than it is because of all the mod 3s.

  1. The choices for B and D can be reduced to
    (- a – c) Mod 3
    and
    (c – a) Mod 3

    respectively. This is nice and symmetric as one would hope.

  2. Jim "James" Jimson

    Hi Arvind,

    This is a nice write-up. I believe there might be a simpler way to solve this however. Let me know what you think of the following solution.

    Suppose the vertices of the square are 1-2-3-4 so that 1 is adjacent to 2 and 4, 2 is adjacent to 1 and 3, 3 is adjacent to 2 and 4, and 4 is adjacent to 3 and 1.

    The players should use the following strategy when guessing the color of their hat:

    1. If a player sees two different-color hats, then he should guess the remaining color for his own hat. E.g., if player 1 sees that 2’s color is Red and 4’s color is Yellow, then he should pick Blue. [Note that this is what 3 will guess as well.]

    2a. Players 1 and 3: If they see hats of similar colors, then player 1 chooses one of the remaining colors and player 3 the other. E.g., if they see two Red hats, player 1 guesses Yellow and player 3 guesses Blue. These choices of players 1 and 3 are agreed upon beforehands, for all three colors.

    2b. Players 2 and 4: If they see hats of similar color, then they should guess that color. So, if they see two Red hats, they should both guess Red.

    If I’m not mistaken, this strategy ensures that at least one player guesses correctly.

    1. Hi Jim, I’m not sure this works: consider hat color arrangements (0,1,1,1) for players 1-4 as you’ve arranged them. In this case players 1 and 3 see the same hat on color 1 on player 2 and 4. So by your rule 2a. let’s say they guess 1,2 for now, based on previously agreed strategy. Players 2 and 4 both guess color 2 based on rule 1. So their combined guesses are (1,2,2,2). So this doesn’t work. Now you might say what if players 1 and 3 had agreed on picking colors 2, 1 instead when seeing the same color 1 on players 2 and 4. So in this case they would pick (2,2,1,2) but this would not work in the case of the actual colors being (1,1,0,1).

      1. Jim "James" Jimson

        You are absolutely right. I did this quickly and didn’t notice that there were two types of cases when three players have the same color.
        Good job on your solution.

  3. You can use linear algebra to brute force this. Work mod 3 and assume your guess is a linear combination of the colours you see. This can probably be generalized to other arrangements (not just a square I suppose). Note there might be other solutions which are not linear combinations (like bd + b + d etc).

    Write an equation for how off you are. Eg

    delta_a = xb + yd – a

    This gives

    A [a,b,c,d] = [delta_a, delta_b, delta_c, delta_d] where A is the matrix corresponding to the strategy (basically each choice of the different x,ys gives rise to a corresponding A). If for some A, for every x = [a,b,c,d], at least one co-ordinate of Ax is zero, then we have a viable strategy.

    I wrote python code to bruteforce this and got 8 solutions, but due to symmetry it seems like they are essentially the same:

    a -> b + d
    b -> 2a + c
    c -> 2b + d
    d -> 2a + 2c

    Maybe there is a clever math way of coming up with some A and proving that for every x, some co-ordinate of Ax is 0.

    You can also consider using polynomials in 4 variables: multiply the deltas and try to get a polynomial that you can show is identically 0 (note mod 3, with x^3 = x, x^2 = 1 for x != 0) etc.

    1. Thanks PJ. Yep your solution works! I’ve edited my post to call this out as an alternative approach, credited to you of course.

Comments

%d bloggers like this: