Flipping the Glasses

In the last post, I mentioned a simple logic puzzle that I’d stumbled over while wandering around the interweb. Start with four glasses, in a row, all upright. The goal is, through a series of moves, to turn them all upside down. On each move, you flip three of the four glasses — up if down or down if up.

The goal is to end up with all four glasses upside-down in the least number of moves possible. It’s not hard to find the solution by trial and error, but it turns out there’s an underlying trick that not only solves it but solves it regardless of the number of glasses (where each move flips N-1).

Bonus: these solutions even look pretty — or at least symmetrical.

We’ll start with the original four-glass puzzle and then expend to the general case for any number of glasses. One punchline up front, it turns out to be any number of even glasses. There are no solutions for odd numbers of glasses.

We’ll start with four glasses (N=4), in a row, all upright:

State 0: Initial state, all upright.

The only rule is that we flip three of them each move. With all upright, it doesn’t really matter, they’re all the same, so we’ll flip the first three:

Move 1: We’ll flip the first three glasses.


Which gives us:

State 1: Three down and one up after one move.

The question of which three to flip is more challenging this time. Clearly, if we flip the same first three, we’ll end up where we started, so that’s no useful.

The only alternative is to flip the upright glass on the right and then two of the upside-down glasses on the left:

Move 2: The one (different) glass from right plus two from the left.


Which gives us:

State 2: Two down and two up after two moves.

I’ll mention that this set of moves follows the protocol of the underlying trick for solving the puzzle. I’ll cover it in more detail below, but it basically consists of flipping as many glasses on the right as are different from the glasses on the left and then taking the remaining flips from the glasses on the left. In both cases, start with the glasses on the outside and work in.

Note that flipping the two on the left, plus one on the right, would put us back in state one, with three down and one up. Following out protocol, though, we want to flip the two on the right and one more from the left:

Move 3: The two from the right plus one from the left.


Which gives us:

State 3: One down and three up after three moves.

Which, ah ha, obviously just needs one more move to reach the final state (of all glasses upside-down). This is the final state necessary — one glass already upside-down and N-1 glasses (three, in this case) to flip.

It’s the only final state that does lead to a solution, which means the state that leads to it is the only state leading to leading to a solution, and so on back to the initial state. Thus, these are the minimal moves required.

We flip the last three:

Move 4: Flip the last three (and none from the left).


And that gives us:

State 4: All glasses are upside-down.

The final state, our goal. It took four moves, and it turns out that it always takes as many moves as there are glasses. The pattern of moves may not be readily apparent in the small set of glasses above, so let’s consider some bigger sets of glasses.

§

Before I show you an example with ten glasses (N=10), consider the pattern in the examples above. As mentioned, the rule is to flip as many glasses on the right (starting from the right side) as are different from the glasses on the left. Since a move requires flipping N-1 glasses, flip as many glasses on the left (starting from the left side) as required to add up to N-1 glasses flipped.

On the first move, no glasses on the right are different, all the glasses are the same: upright. So, we flip zero glasses from the right and take all N-1 glasses from the left. Which is why we flip the left three glasses on the first move.

On later moves, there are glasses on the right that are different from glasses on the left — either being upright or upside down — so we flip all the glasses on the right that are different. Note how this grows from zero the first time to three the last time. Each move flips one more glass from the right and one less glass from the left.

On the final move, there are three (N-1) glasses on the right that are different from the one remaining glass on the left. So, this time we take all N-1 glasses there and zero from the left.

Lastly, note how flipping N-1 this means one glass is not flipped each move. That glass starts on the far right and moves one position left each move until it’s on the far left. You can think of the pattern as flipping all the glasses each move except for this lone moving glass.

Regardless of how you view the protocol, it takes N moves to flip N glasses.

§

With that in mind, here are the ten moves required to flip ten glasses:

The 10 Glasses Solution. [click for big]

To make the pattern a bit easier to see, the upright glasses are red, and the upside-down ones are blue. Note how the penultimate move has nine (N-1) glasses ready for the final move (and is symmetrical with the first move — in fact, there is a forward-backward symmetry through the entire progression).

Also note how you can always find a single lower glass that matches the glass above it — the one that didn’t get flipped.

§

For what it’s worth, the algorithm looks generically something like this:

--Glasses Puzzle Solver!

-- Start with the number of glasses...
N = 12

-- Define a row of N glasses...
Glasses = [0]*N

-- Print the starting position...
print(Glasses)

-- Function to flip a given glass...
function flip_glass (index) begin
    Glasses[ix] == not Glasses[ix]
end

-- Make N moves to turn all glasses upside-down...
for move = 1 to N begin

    -- Flip glasses on the right...
    if 1 < move then begin
        for right = 1 to move-1 begin
            flip_glass(N-right)
        end
    end

    -- Flip glasses on the left..
    if move < N then begin
    for left = 1 to N-move begin
        flip_glass(left)
        end
    end

    -- Print the move...
    print(Glasses)

--Done!
end

Which is basically the same protocol described above. Rather than actually examining the glasses on the right to see which ones differ from glasses on the left, the algorithm recognizes that the number of glasses flipped on the right starts with zero and grows to N-1 while the number of glasses flipped on the left starts with N-1 and decreases to zero.

In fact, it is that simple. On the right, start with zero (glasses flipped) and increase to N-1, one glass per move. On the left, start at N-1 (glasses flipped) and decrease to zero, one glass per move. These are exactly anti-symmetrical (one increases while the other decreases), and both require N moves.

§

That’s all there is for now. I’ll leave you with a 42-glass version:

Click for a 3360×5762-pixel version!

Stay flipped, my friends! Go forth and spread beauty and light.

About Wyrd Smythe

The canonical fool on the hill watching the sunset and the rotation of the planet and thinking what he imagines are large thoughts. View all posts by Wyrd Smythe

11 responses to “Flipping the Glasses

  • diotimasladder

    Whoa, that last one has a diagonal line in it. You don’t see that with the 10 glasses (which looked sort of like a kind of crochet stitch pattern).

    • Wyrd Smythe

      That diagonal is there in the other examples, too. That it’s a straight diagonal is a consequence of the protocol of taking the different glasses from the right. Each move, there is one more different glass on the right, so the number of flips grows by one, forming the diagonal line.

      Overall, the pattern flips colors (in the red/blue ones) each time, but the “break” of the diagonal line splits the color flipping. On the first move, with all glasses red, all but one of them flip to blue. On move two, all but two of them flip back to red. On move three, all but three of them flip to blue, and so on. At the end, it’s down to none of this group flipping. Meanwhile, there’s a group that flips the colors the opposite way growing on the right. At first, none of group on the right flip, but by the last move, all but one flip. So, you’re seeing an opposing color flip migrate from left to right, along that diagonal.

      That diagonal, by the way, also shows why it always takes N moves to flip N glasses. The diagonal has to go from right to left, one glass at a time, and that takes N moves.

      • diotimasladder

        Now that I’m on my laptop I can sort of see the diagonal for 10 glasses. 🙂 It’s not something I would’ve noticed without knowing it was there.

      • Wyrd Smythe

        That’s kind of why I made the big one, to show off that pattern. The Python app I wrote to exercise the protocol has output that makes the diagonal really obvious, even with just ten glasses:

         0: [UUUUUUUUUU]  0/10
         1: [_________U]  9/ 1
         2: [UUUUUUUU__]  2/ 8
         3: [_______UUU]  7/ 3
         4: [UUUUUU____]  4/ 6
         5: [_____UUUUU]  5/ 5
         6: [UUUU______]  6/ 4
         7: [___UUUUUUU]  3/ 7
         8: [UU________]  8/ 2
         9: [_UUUUUUUUU]  1/ 9
        10: [__________] 10/ 0
        

        The ‘U’ is an upright glass, a ‘_’ is an upside down one. The numbers on the left are the moves, the number pairs on the right are the number of upside-down versus upright glasses. There’s an interesting pattern in those, too!

      • diotimasladder

        Oh yeah, it is really obvious!

  • Katherine Wikoff

    “In fact, it is that simple.” I love it! I love that even though my brain started to hurt from following all the flips and logical twists, we finally got to the place where it all makes sense. I think this same experience must be why I enjoy reading mystery novels. I don’t mind the pain of thinking hard if I can reasonably expect a satisfying explanation or insight at the end. Anyway, now I know what to do if I ever find myself in a situation where I’m challenged to flip glasses like this. It’s good to have skills 😀

    • Wyrd Smythe

      Even if you never have to use the skill, the process of acquiring intellectual tools is good for the brain! Solving the puzzle at hand (four glasses) is already good exercise. Generalizing it takes it to a higher level — some mental chewing time. I figure it’s helping me stave off senility! 🤓

      And I like that it is simple (as in: simple enough for me to figure out). It’s somewhat related to the Tower of Hanoi puzzle, which also has a simple (albeit tedious) process that’s the key to solving any size tower.

  • headbirths

    Nice one.

    Back in my alcohol drinking days, I got people to do this with coins in the past: 3 coins, from 3 tails to 3 heads, flipping only 2 at a time. Very frustrating for them when they see you do it in 3 moves but can’t do it themselves.

    • Wyrd Smythe

      Huh. You’re going to have to show me how it’s done, because I can’t make it work. Starting with T-T-T, if I flip two, I have H-H-T. If I flip the two heads, I’m back where I started. If I flip the one tail and a head, I have H-T-H, which is no change — two heads, one tails. 😕

      Is this one of those bar gags where the solution is decidedly out of the box (or even out of bounds)? I was given to understand that the puzzle cannot be solved for odd numbers.

      • headbirths

        You are absolutely right!

        Flipping 2 coins will either add 2 to the number of heads, subtract 2 or leave it unchanged. Starting with zero heads, you can only ever have zero or 2 heads, never 3.

        The solution is…

        When *you* demonstrate it to them, do it quickly so they don’t notice you actually start with 3 heads!

        Turn the three coins ‘back’ to tails for them to try.

        They fail of course.

        Pick the 3 coins up from their side of the table. Them put them down all-heads on your side and do it very quickly. Possibly pass them from one hand to the other before putting them down if this makes it easier to flip them without them noticing.

        I can never believe I’ll get away with it before trying it on others but it does! It is even better if there is someone else in on it to ‘independently’ show it can be done. And some alcohol helps.

        Give it a try. Cheers!

      • Wyrd Smythe

        Ha, that’s excellent! 😂 I’ll have to try that with someone sometime. It really does seem as if people should notice, but I can see it sliding right past people, too. And, damn, now there’s no way to test it on me. I’d like to think I’d notice, but… 🤷🏼‍♂️

        At least my faith in math is restored. There are no (legit) solutions for an odd number. 😅

And what do you think?