So I just wanted to solve an amazing extension of this puzzle that I happen to know of.
Suppose that there were more than two colors. In fact, let's suppose that there were an uncountably infinite number of colors (so we're effectively writing a real number on each hat). And of course, we still have a countably infinite number of people in line. Is there a strategy that permits only a finite number of people guessing wrong?
Amazingly, the answer is still yes. The answer uses some abstract algebra, but I'll try to simplify it.
Firstly a relation is basically a relation between two things. For example, the relation "is greater than" is $>$, and we have statements such as $2>1$, $3>2$. If we define a relation $R$ to be "has a crush on" then $\text{Me}\ R\ \text{Grace}$ would be true, but $\text{Grace}\ R\ \text{Me}$ probably isn't.
There is a special type of relation called an equivalence relation. It's a relation that is very similar to equality. Some properties of equality that you may remember are:
- $a=a$ for any number $a$. (Reflexive Property)
- If $a=b$, then $b=a$. (Symmetric Property)
- If $a=b$ and $b=c$, then $a=c$. (Transitive Property)
So, equality is a relation that is reflexive, symmetric, and transitive. Similarly, a relation $R$ is an equivalence relation if it satisfies these three properties.
For example, the relation $M$, defined as "is in the same math class as" is an equivalence relation, assuming everyone takes exactly one math class. So $\text{Me}\ M\ \text{Sophia}$ is true because Sophia and I both take AP Calculus BC. Likewise, it should be obvious that $\text{Sophia}\ M\ \text{Me}$, so we have the symmetric property. I'm in the same math class as myself, i.e. $\text{Me}\ M\ \text{Me}$, so we have the reflexive property. Lastly, since $\text{Sophia}\ M\ \text{Allison}$, it is obvious that $\text{Me}\ M\ \text{Allison}$, confirming transitivity. These three properties imply that $M$ is indeed an equivalence relation.
The relation $R$, defined as "has a crush on", is not an equivalence relation, because it violates all three conditions. I don't particularly like myself, and nobody that I have a crush on feels the same way, so we have many counter-examples here.
The relation $C$, defined as "is within one mile of", is not a equivalence relation either. While it is true that $\text{Me}\ C\ \text{Me}$, and $\text{Me}\ C\ \text{Person B}$ implies $\text{Person B}\ C\ \text{Me}$, the transitivity condition does not hold. That is, just because $A$ and $B$ are within one mile, and $B$ and $C$ are within one mile, does not mean that $A$ and $C$ are within one mile.
Equivalence relations are extremely important because they can split up a set into equivalence classes. Let's consider again the math class relation $M$. Notice how it literally splits all students in my school into classes. In each class, you can take any two students $A$ and $B$, and it will follow that $A\ M\ B$. However, no two students $C$ and $D$ in different math classes will satisfy $C\ M\ D$.
In other words, everyone in Ms. Dwyer's Calculus BC class is related under $M$. Likewise, everyone in Mr. Holden's Calculus BC class is related under $M$. However, no two students, one of which is in Dwyer's class with the other being in Holden's class, are related under $M$.
Thus, $M$ splits the students of my high school into equivalence classes, and each equivalence class is a set of students in a literal math class. Another example is the relation $X$, defined as "has the same last digit as". This splits the positive integers into 10 equivalence classes: $\{1, 11, 21, \cdots\}$, $\{2, 12, 22, \cdots\}$, $\cdots$, $\{10, 20, 30, \cdots\}$. Because of how equivalence relations are defined, they will always split sets into disjoint equivalence classes.
Now we're ready to go back to the puzzle. We start by assigning an order to the people, so the colors will form a sequence:
$$\text{Red, Green, Blue, Fuschia, Brick Red, }\cdots$$
We define a relation $R$ on the set of hat color sequences. Two possible hat color sequences $A$ and $B$ satisfy $A\ R\ B$ if they are eventually the same after a finite number of terms. That is, consider the two sequences:
$$A = \text{Red, Green, Blue, Fuschia, Brick Red, Green, Purple, Hot Pink, }\cdots$$
$$B = \text{Black, White, Magenta, Fuschia, Brick Red, Green, Purple, Hot Pink, }\cdots$$
For a few colors, $A$ and $B$ aren't on the same page. But eventually, they are the same for the rest of the sequence! (I know I didn't show an infinite number of terms in each sequence to convince you, but just trust me lol)
Is $R$ an equivalence relation?
Well, any sequence is the same as itself. And, if $A$ and $B$ are eventually the same, then $B$ and $A$ are eventually the same. Finally, if $A$ and $B$ are eventually the same, and $B$ and $C$ are eventually the same, it will follow that $A$ and $C$ are eventually the same. Thus, $R$ is an equivalence relation. That means $R$ splits the set of possible hat color sequences into equivalence classes.
Now what's the strategy? The countably infinite number of people, before the puzzle starts, will discuss. They define the relation $R$, and note the infinite number of equivalence classes created by $R$ on the set of possible hat color sequences. Then, they invoke the Axiom of Choice by choosing a representative element from every class.
For example, for the (only!) equivalence class that contains the sequence $\text{Blue, Green, Red, Red, Red, Red, Red, }\cdots$ (that is, the set of sequences that all eventually become completely Red), they may choose the representative $\text{Hot Pink, Fuschia, Magenta, Red, Red, Red, Red, }\cdots$. Everyone makes sure that they agree on which representative to choose, and that they remember exactly which representative to choose given the equivalence class.
Now the game starts, and the plan is set into action. They stand in the agreed order, and open their eyes. Suddenly, everyone can see the infinite hat colors down the line. That means that everyone knows which equivalence class this particular hat sequence is in. And, everyone remembers which representative to choose from this equivalence class. Everyone then guesses their hat color in accordance to this representative.
For example, if the third person sees:
$$\text{Green, Blue, ???, Blue, Red, Red, Red, Red, Red, Red, }\cdots$$
Then that third person knows they are in the "eventually all red" equivalence class, and recalls the agreed-upon representative sequence $\text{Hot Pink, Fuschia, Magenta, Red, Red, Red, Red, }\cdots$. That means the third person will guess Magenta. Likewise, the two people behind him will guess Hot Pink and Fuschia, while everyone else guesses Red.
Why does this strategy work? Exactly because of the way we defined $R$. If we know that our representative sequence is eventually the same as the actual sequence after a finite number of terms, then only a finite number of people could possibly guess wrongly until they start getting the rest of the colors right. In the example above, the first four people were dead-wrong, because the actual sequence was:
$$\text{Green, Blue, Sewage Green, Blue, Red, Red, Red, Red, Red, Red, }\cdots$$
And the chosen representative sequence that they used to guess was:
$$\text{Hot Pink, Fuschia, Magenta, Red, Red, Red, Red, }\cdots$$
But both sequences were eventually the same after a finite (4) number of terms, so all the people that starting guessing red from the fifth person onwards got it right.
Yeah math makes no sense. And yet it does, somehow. Isn't that great?