TITLE: A riddle with a witch and some gnomes (guessing numbers on hats)
QUESTION [24 upvotes]: My question concerns a variation and a generalization of the following riddle.
The Original Riddle:
A wicked witch kidnaps 2 gnomes. She paralyzes them, and places a hat on each of their heads. Each hat either has the number $1$ or the number $2$ written on it (both hats may have the same number). She then places the gnomes so they face each other (each gnome sees the number on the other gnome's hat). Afterwards, she puts the gnomes in separate rooms, where she undoes the paralysis, and asks each of the gnomes to guess which number is written on their own hat. If at least one of the gnomes guesses right, she'll let them go, otherwise she'll use them to make a gnome-stew. Luckily for the gnomes, they knew about the witch's plan, and they planned ahead. They prepared a strategy that will always guarantee that at least one of them guesses right. What was their strategy?
Solution:
The first gnome (call him gnome $A$), will always select the opposite of what he saw on the second gnome's (call him gnome $B$) hat, while gnome $B$ will always select the same value that he saw on gnome $A$'s hat. Putting this strategy in a truth table shows that it always works:
$$
\begin{array}{c|c|c|c}
\text{Gnome A's Hat} & \text{Gnome B's Hat} & \text{Gnome A's Guess} & \text{Gnome B's Guess} \\
\hline
1 & \color{lime}1 & 2 & \color{lime}{1} \\
\color{lime}1 & 2 & \color{lime}1 & 1 \\
\color{lime}2 & 1 & \color{lime}2 & 2 \\
2 & \color{lime}2 & 1 & \color{lime}2 \\
\end{array}
$$
One can also give a more intuitive explanation for the solution: Gnome $A$ gambles that both gnomes have different valued numbers on their hats, while gnome $B$ gambles that both gnomes have equal values numbers in their hats. Since these are the only possibilities, at least one of them must be correct.
A  Variation:
How can the riddle be solved with 3 gnomes, and hats with 3 values ($1$, $2$ and $3$), where each of the gnomes sees the values on the other two gnomes' hats? And is there an intuitive explanation for the solution?
Partial Answer:
Trying out some values I found the following strategy solves the problem for 3 gnomes:
$$
% outer vertical array of arrays
\begin{array}{c}
% inner horizontal array of arrays
\begin{array}{cc}
% inner array of minimum values
\begin{array}{c|c|c}
\text{A's Hat} & \text{B's Hat} & \text{C's Guess} \\
\hline
1 & 1 & 3 \\
1 & 2 & 3 \\
1 & 3 & 1 \\
2 & 1 & 3 \\
2 & 2 & 1 \\
2 & 3 & 2 \\
3 & 1 & 2 \\
3 & 2 & 2 \\
3 & 3 & 1
\end{array}
&
% inner array of maximum values
\begin{array}{c|c|c}
\text{A's Hat} & \text{C's Hat} & \text{B's Guess} \\
\hline
1 & 1 & 2 \\
1 & 2 & 1 \\
1 & 3 & 3 \\
2 & 1 & 1 \\
2 & 2 & 2 \\
2 & 3 & 2 \\
3 & 1 & 1 \\
3 & 2 & 3 \\
3 & 3 & 3
\end{array}
\end{array}
\\ \\
% inner array of delta values
\begin{array}{c|c|c}
\text{B's Hat} & \text{C's Hat} & \text{A's Guess} \\
\hline
1 & 1 & 1 \\
1 & 2 & 2 \\
1 & 3 & 3 \\
2 & 1 & 3 \\
2 & 2 & 1 \\
2 & 3 & 3 \\
3 & 1 & 2 \\
3 & 2 & 1 \\
3 & 3 & 2
\end{array}
\end{array}
$$
Where the third column in each table represents a gnome's guess, given what he sees on the other two gnomes' heads. This strategy always works, as indicated in the truth table below:
$$
\begin{array}{c|c|c|c}
\text{A's Hat} & \text{B's Hat} & \text{C's Hat} & \text{A's Guess} & \text{B's Guess} & \text{C's Guess} \\
\hline
\color{lime}1 & 1 & 1 & \color{lime}1 & 2 & 3 \\
1 & \color{lime}1 & 2 & 2 & \color{lime}1 & 3 \\
1 & 1 & \color{lime}3 & 3 & 3 & \color{lime}3 \\
1 & \color{lime}2 & 1 & 3 & \color{lime}2 & 3 \\
\color{lime}1 & 2 & 2 & \color{lime}1 & 1 & 3 \\
1 & 2 & \color{lime}3 & 3 & 3 & \color{lime}3 \\
1 & 3 & \color{lime}1 & 2 & 2 & \color{lime}1 \\
\color{lime}1 & 3 & 2 & \color{lime}1 & 1 & 1 \\
1 & \color{lime}3 & 3 & 2 & \color{lime}3 & 1 \\
2 & \color{lime}1 & 1 & 1 & \color{lime}1 & 3 \\
\color{lime}2 & 1 & 2 & \color{lime}2 & 2 & 3 \\
2 & 1 & \color{lime}3 & 3 & 2 & \color{lime}3 \\
2 & 2 & \color{lime}1 & 3 & 1 & \color{lime}1 \\
2 & \color{lime}2 & 2 & 1 & \color{lime}2 & 1 \\
2 & \color{lime}2 & 3 & 3 & \color{lime}2 & 1 \\
\color{lime}2 & 3 & 1 & \color{lime}2 & 1 & 2 \\
2 & 3 & \color{lime}2 & 1 & 2 & \color{lime}2 \\
\color{lime}2 & 3 & 3 & \color{lime}2 & 2 & 2 \\
3 & \color{lime}1 & 1 & 1 & \color{lime}1 & 2 \\
3 & 1 & \color{lime}2 & 2 & 3 & \color{lime}2 \\
\color{lime}3 & 1 & 3 & \color{lime}3 & 3 & 2 \\
\color{lime}3 & 2 & 1 & \color{lime}3 & 1 & 2 \\
3 & 2 & \color{lime}2 & 1 & 3 & \color{lime}2 \\
\color{lime}3 & 2 & 3 & \color{lime}3 & 3 & 2 \\
3 & 3 & \color{lime}1 & 2 & 1 & \color{lime}1 \\
3 & \color{lime}3 & 2 & 1 & \color{lime}3 & 1 \\
3 & \color{lime}3 & 3 & 2 & \color{lime}3 & 1 \\
\end{array}
$$
However, I could not find any intuitive explanation for this solution, nor could I prove that this is the only solution. This motivates the first part of my question:
Is there a more intuitive way to look at the given solution for 3 gnomes, and is this the only solution?
The second part regards a generalization of the question:
Is there a general technique that can be used to solve this question for $n$ gnomes and hats with $n$ values?

REPLY [4 votes]: For the general question there is a simple method: The $k$th gnome assumes that the sum of the numbers is $k$ modulo $n$, that is, the sum of the numbers minus $k$ is divisible by $n$. With that assumption, he knows what his number is if he assumed correctly. Because one of the gnomes has to have the right assumption, one of them makes the correct guess.