# Magical Shuffling Hats

As you're walking through the city, a wandering street logician pulls you aside. "Hey, you. Want to play a game?" He rasps in a loud whisper. You, being quite the logician yourself, oblige. He leads you through a dark, winding alley, eventually ending at a secluded, run-down storage locker. Shuffling a bit, he unlocks the rusty door and opens it with a loud creak, revealing darkness behind it. He gestures you forward, and you step in. As your eyes begin to adjust to the darkness, you jolt as the door slams shut behind you. No sooner has it shut than the lights turn on, revealing six hats of different colors, placed on stools. Taking a quick inventory, you note that the hats in order from left to right are Red, Blue, Yellow, Green, Orange, and Purple.

"So here's the game," the logician wheezes. "As of now, you are trapped in this locker with me. The only way I'll let you out is if you can follow the Magical Hats! I'm going to shuffle the hats, then give you clues about their whereabouts. If you can correctly identify where each hat has finished, you with obtain your freedom, as well as the hat of your choice! Incorrectly identify the hats, well...let's just say I hope you correctly identify the hats."

You flex your brain muscles. Time to play hardball.

The logician lets a drape loose, shielding the hats from your view. He runs behind the drape and begins frantically moving hats around, panting as he runs between stools. For awhile you attempt to track the hats, but you give up as his movement is too erratic to follow. Finally, he stops and comes out from behind the curtain.

"Here's what I have to tell you about the new order of hats," he pants.

1. Exactly one hat is in the same place that it started.
2. Exactly one pair of hats have swapped places with one another.
3. The first three hats in the original display are now in the odd-numbered locations.
4. The Red hat moved the undisputed farthest out of all the hats.
5. The Blue hat is adjacent to its starting position.
6. The Purple hat is now where the Green hat began.

You think for a second, then begin to piece together where the hats are. This is too easy, you muse with a smirk. This logician doesn't know what he has coming. As you put the last piece of the puzzle into place and begin to speak, the logician cuts you off.

"Wait, wait, wait! This is TOO easy!" He shrieks. In haste, he runs behind the curtain and begins shuffling the hats again. You feel your morale break, as everything you had put together has just been shattered. Still, you steel yourself, and prepare for his next challenge.

After an eternity, the logician once again returns from behind the drape. "Okay, this time...this time I have it right," he gasps. "And here are your clues."

1. Exactly one hat is in the same place that it started.
2. Exactly one hat is in the same place that it was in the previous iteration of hats.
3. Exactly one pair of hats have swapped places with one another from the previous iteration of hats.
4. All hats that were adjacent to their starting position in the previous iteration of hats are still adjacent to their starting position.
5. There is only one set of hats that started adjacent and have remained adjacent.
6. The hat on the far left has not been on an end until now.
7. The left half and the right half from the previous iteration contain the same assortments of hats.

You take a minute to breathe. Let's see if I can still do this, you wonder as your brain begins to pulse. Just as you begin to unravel the strings of the hat mystery in your mind, the logician shrieks at you.

"WAAAAAAAAAIT!!!!"

Your gears halt. "What?" you spit at the logician angrily. He flinches backwards, then sheepishly admits, "I forgot to tell you about the lucky and unlucky hats."

You groan.

He continues on, "The lucky hat is worth seven million dollars - it's made of quite a rare silk, you see. The unlucky hat, however, will literally kill you instantly if you put it on your head." Before you can posit a conjecture about simply not wearing the hat, he adds, "I of course will not let you leave until you are wearing your selected hat."

How can I possibly know which is which? You think with a furrowed brow. Luckily, he has prepared an answer for that as well. "Unfortunately, I don't remember which hat is which, but I do remember a few things about the hats..."

"...and here's what I remember."

1. Of the lucky and unlucky hats, one is a primary color and one is a secondary color.
2. At no point while the hats were at rest were the two hats next to one another.
3. If the lucky hat is a primary color, then the unlucky hat was never at rest on either end.
4. If the lucky hat is a secondary color, then the unlucky hat is yellow.

You wait, as he stands in thought. "No, that's all I remember," he cedes after a few seconds.

You groan again. Time to get to work.

Where were the hats after the first shuffle? Where were the hats after the second shuffle? Finally, when you win the prize and are allowed to leave, which hat do you take with you?

• I wonder if the Mastermind tag would be appropriate here? It's basically the same underlying principle AFAICU... – Rand al'Thor May 12 '15 at 21:55
• Too long question! :D difficult for me who is not native english speaker. – Nai Jun 8 '15 at 9:37
• @Nai: I hadn't considered that when I was writing the question! I'll be sure to keep that in consideration next time. :) – Bailey M Jun 8 '15 at 12:54

I get the same answer as the one already posted, but might as well post the workings:

## First Iteration

• We start with starting point RBYGOP and the unknown first iteration which I'll write as ......

• We're told the position of the purple hat, so we can fill it in as ...P..

• From the odd number clue, the red hat can be in the 1st, 3rd or 5th position. Since the purple hat has already moved two, the 5th position is the only one that makes it the undisputed furthest mover. That gives us ...PR.

• From the above, the red and purple hat can't be in the same place they started, nor can the green or orange hats since their previous spaces are now occupied. Also, the blue hat started in an even space but has to be moved to an odd one. From that, we are left with the yellow hat as the only one that can be in the same position as it started. ..YPR.
• The first space is now the only odd one left for the blue hat. B.YPR.
• Finally, the only remaining candidate for a swap is green and purple, giving us BOYPRG

## Second Iteration

• The blue hat is the only one adjacent in the first iteration to the start point. This means it must be in the first or third position. But we also know the leftmost one can't be blue as it's already been on the edge. This gives us ..B...

• The first two spaces must be occupied by O and Y in some order, and the last three by P,R and G in some order. This means the only candidates for starting adjacent and still being adjacent are Y in the second space being adjacent to B, or R in the fourth space being adjacent to B: .YB... or ..BR...

• The first of those two is more restrictive, so try that.
• Now we know the R must be in one of the last two slots because only one adjacent pair can remain from the start.
• R can't be in the penultimate slot because then P and G would either both be in the same position as they started, or both be in the same position as last iteration
• R can't be in the last slot because then we can only satisfy a hat being in the same position as the start or a hat being in the same position as the first iteration but not both.
• This leaves no valid position for R, so we've excluded this possibility and know it must be ..BR..
• Since only one adjacent pair can be the same as the start, we also know Y can't be in the second slot, arranging the first two as YOBR..

• The O is in the same place as it was in the first iteration, so the G can't also be, giving us the final result YOBRGP

## Lucky and Unlucky

• "Primary" is somewhat ambiguous as the primary colours could reasonably be RGB or RYB depending on the colour model.

• Start with assuming RGB. Together the first and four clues rule out the lucky hat being secondary
• This means we're looking for a pair of colours with the following properties:
• One is in {R,G,B}, the other is in {O,Y,P}
• The one in {O,Y,P} was never at rest on either end.
• Yellow and purple have both been on an end, so the latter must be orange
• Red is the only primary colour that has never been next to orange, giving us: lucky red, unlucky orange.
• On the other hand, we can try taking RYB as primary. Consider the case of yellow as unlucky. Then we need a colour satisfying the properties:

• Is in {O,G,P}
• Has never been adjacent to yellow
• That's not satisfied by any colour, so we can rule that out and instead say the lucky hat must be a primary colour. Now we looking for a pair of colours satisfying:

• One is in {R,Y,B}, the other is in {O,G,P}
• The one in {O,G,P} was never at rest on either end.
• Green and purple have both been on an end, so the latter must be orange
• Red is the only primary colour that has never been next to orange, giving us: lucky red, unlucky orange.
• Therefore whichever of the likely options we take for "primary", we get the same result: Lucky red, unlucky orange.
• Interested if anyone has a simpler way of moving forward than the second point in "Second Iteration" – Ben Aaronson May 13 '15 at 11:46
• +1 for a well-explained correct answer! :) I did intend RYB as the primary colors, so sorry for any confusion there - good thing the answer still worked out the same! For step two of the second iteration, note that there has to be exactly one swap. Since you know that B is in the third slot, there are no hats that could be the "one hat in the same place it started" in the first half. The only possible swap that would put a hat in the same place it started in the second half would put TWO hats there (green and purple), so the swap has to be in the first half, and thus be Yellow and Blue. – Bailey M May 13 '15 at 13:13
• @BaileyM Thanks! I guessed that you meant RYB from the fourth clue, but I thought might as well try both and see if one led to a contradiction. Instead they came out the same, which was just as good. And yes, that's a nice way of doing that tricky step – Ben Aaronson May 13 '15 at 13:25

After the first iteration the hats were like so:

Blue, Orange, Yellow, Purple, Red, Green

After the second iteration, the hats were like so:

Yellow, Orange, Blue, Red, Green, Purple

The hat to take is:

Red

The unlucky hat is:

Orange

• Exactly what I got, too – Rob Watts May 12 '15 at 22:14
• Yep, same here. – lorimer May 12 '15 at 22:16
• Downvoted for a lack of explanation; for a puzzle this complex, an explanation is absolutely 100% required IMO – Joe May 13 '15 at 9:52