Large group of dwarfs busted into the dragons cave with an intent to steal his treasures. They were clumsy and during the loot they accidentally woke up the dragon.
Dragon found those little thieves very cute and adorable and decided not to kill them, instead he has given them an assignment. Using magic he placed little hats on every dwarf’s head (so every dwarf was wearing either a black or a white hat) and then he told them to walk out of the cave and align themselves in one row, side by side so that all the dwarfs with the white hat on stand on one side of the row, and all the dwarfs with the black hat on at the other end of the row.
None of the dwarfs knew what color is their own hat. There were no mirrors in the cave. Dragon used a magic spell to prevent any kind of communication between the dwarfs (no talking, no whistling, no coughing, no sneezing, no blinking, no gestures of any kind). All they could do is think and walk out of the cave to assume their position in the row.
If they manage to do what dragon assigned them to do he will let them take all the treasures they can carry with them!
The dwarfs did manage to complete the task... how??
Please use spoilers!