The puzzle
You and your friends are walking through the park when an evil game master kidnaps you all and forces you to play a devious game.
The game master sits you all down in a room across from one other and explains the game. The game master says that he will be placing hats on each of your heads. Each hat will be one of several colors, the maximum number of possible colors will match the number of people in your group. So if there are six of you, there will be six different possible colors that the game master can choose from. However, the game master is sneaky, and can choose to use as few or as many colors as he sees fit; some of you could end up wearing the same color hat. You won't be able to see your own hat, but you will be able to see everybody elses.
Once the game master places all the hats, then one by one he will ask you in order to write down your best guess as to what your own hat color is. If at least one of you is able to guess your own hat color, he'll set you all free, and if not, you all must remain his prisoners!
Feeling merciful, the game master gives you a few moments to say goodbye to each other as he believes you are all doomed. This gives you a few moments to think of a way to try and win his devious game. This is important because once the game master sits you down, you won't be able to communicate at all to each other without forfeiting the game.
How can you guarantee that at least one of you will guess your own hat color correctly?
TLDR
Rainbow Hats Puzzle
- You and friends are sitting in a room across from one another.
- Each of you is wearing a colored hat .
- The number of unique hat colors is equal to .
- Colors may or may not repeat.
- You can see everyone elses hat color except your own.
- No friend can communicate with another once the hats are placed.
- No friend can hear any other friends guesses
Goal: garuntee that at least one of you can guess their own hat color!
Try to solve it for yourself, or keep scrolling for the answer!
The Solution
This has been one of my favorite math puzzles I've seen over the past year. In my mind it strikes that beautiful in between of mathematical simplicity, and hard reasoning. Let's go through that reasoning and the solution step by step:
First lets define some core variables based on the puzzle description
Variable | Description |
The total number of people | |
The ith colored hat (represented as an integer between 0 and n-1) | |
The sum of colored hats seen by person "i", or where |
The first logical and simplest step is to try and express our desired in an algebraic form from the perspective of any one of the friends like so:
Here we are expressing as the difference between the sum of total colors in the group , and the sum of colors any one friend can see .
Now the only variable any one of the friends know is (since they can each see each others hats), and since our final goal is to solve for , the only unknown we need to reason about is .
Here we need to make a small leap of insight. The game master by choosing some combination of colored hats, also decides the value which is bounded by . is tricky to understand because the game master effectively is able chooses any number in this bound. So what do we do? In situations like this where we have a complicated variable to reason about, it's often advisable to try breaking it down or representing it as something more digestible with different constraints to try and make forward progress.
One of the simplest ways to re-represent bounded variables like is by turning it into a linear form like so:
for some a and b with the following constraints: . In this form, no matter what value is, there will always exist a unique a and b equal to that total.
Substitute everything together, we get:
It might seem like we've gone backwards by adding two more "unknowns" to our equation, but by doing this we've added more ways to tackle our problem and integrate new information to help us solve it.
At this point we need to make yet another small leap of insight. Mentioned in puzzle definition is the variable which tells us that all of the friends have an order, or an "id". This is information we can use!
If we look closely, since every friend has a unique , this means that no matter what
the game
master chooses, if we represent in the linear form, exactly one
of the friends
will have an
that is equal to (since will be bounded from to ). Knowing this we can do the following
cancellation:
And almost like magic, we've substituted one of our unknown terms with a term we do know, .
This leads us to one last piece of insight we need to make to get our final answer. We know is bounded by , which means if we apply , it will have no effect. However if we apply it to both sides, we will actually be able to eliminate the term entirely (because any multiple of is just 0 in ).
By applying this operation we get our final expression:
And we've arrived at an interesting conclusion! In order to get a single friend to guess their own color, all we have to do is have each friend take their index, subtract the sum of the colors they see, and mod by ! In doing this we guarantee that one of the friends will correctly guess their own hat!
In this final form, all any friend has to do is:
And one will guess correctly!
Congratulations! The game master, stupefied that one of you was able to guess correctly, begrudgingly releases you!
Thanks for reading! (◍>◡<◍)⋈。✧♡