<s>[INST] A logic problem. No need for calculation Three people were told to go to a cave and each pick up a hat in pitch dark. The three then came out of the cave with the hat on their heads. There were five hats in the cave. Three of them are black, the rest is white. When the first one is asked to tell the color of his hat, he said that he doesn't know. The second person told then same. But the third person said he knows. Why does he know the color of his hat based on what he heard from the other two persons? And how?
 [/INST] Initially there are $7$ possibilities: 
$$
\begin{array}{|c|c|c|c|}
\hline
1 & 2 & 3 \\ \hline
B & B & B \\ \hline
B & B & W  \\ \hline 
B & W & B \\ \hline
B & W & W \\ \hline
W & B & B \\ \hline
W & B & W \\ \hline
W & W & B \\ \hline
\hline
\end{array}
$$
First says that he doesn't know, so one of $2-3$ has a black, that leaves us with:
$$
\begin{array}{|c|c|c|c|}
\hline
1 & 2 & 3 \\ \hline
B & B & B \\ \hline
B & B & W  \\ \hline 
B & W & B \\ \hline
W & B & B \\ \hline
W & B & W \\ \hline
W & W & B \\ \hline
\hline
\end{array}
$$
Second says he doesn't know, that means one of the others has a black, which leaves us with:
$$
\begin{array}{|c|c|c|c|}
\hline
1 & 2 & 3 \\ \hline
B & B & B \\ \hline
B & B & W  \\ \hline 
B & W & B \\ \hline
W & B & B \\ \hline
W & W & B \\ \hline
\hline
\end{array}
$$
Moreover, the second knows that the first has seen a black among $2$ or $3$ and he sees that $3$ has white in row $2$, so if row two is the case then the second actually does know his color, so row $2$ gets eliminated:
$$
\begin{array}{|c|c|c|c|}
\hline
1 & 2 & 3 \\ \hline
B & B & B \\ \hline
B & W & B \\ \hline
W & B & B \\ \hline
W & W & B \\ \hline
\hline
\end{array}
$$
The third knows that his color is black, because in all remaining worlds that's the case.
</s>