# Rotating numbers in a 3x3 grid

We have a 3x3 grid numbers like so

9 8 7
6 5 4
3 2 1

Four numbers in any 2x2 sub-grid can be rotated clockwise or anti-clockwise. For example

a b
c d

rotated clockwise becomes

c a
d b

Is it possible to make a series of such rotations to obtain the following grid?

1 2 3
4 5 6
7 8 9

Bonus question: if it is possible, then what is the fewest rotations required?

Here is a similar puzzle: Rotating numbers in a 2x3 grid

• Is there an intended 'aha moment' to solve this? If not, this seems more like a programming challenge than a puzzle.
– Deusovi
Feb 15, 2020 at 17:29
• Here we go again 😥 I already said you may need a computer to solve it and added the computer tag. Yes I write puzzles that often require a computer to solve. Yes they are still puzzles and they still have aha moments to solve EVEN when you use a computer. Computer solutions can be just as difficult to come up with as manual ones. Now if you don't like such puzzles than that's your own personal choice, but others may like them. Can you please get off my back and not make comments every time. Feb 15, 2020 at 20:42
• There was a recent question for which I crafted a C program to solve it, in conjunction with making a cardboard rig to experiment with by shuffling the parts on squared paper. The accepted answer was numerically better, but the poster said "I used a nonlinear optimization solver" as though it was a tool available to mathematicians. So I can understand some people's aversion to a computer solution. In that case, I made everything (except the cardboard and the compiler) with my own hands and brain. Feb 15, 2020 at 20:55
• Yes, it's probably possible to optimize your algorithm in clever ways, but there don't appear to be any built-in "aha moments". It seems that brute force would work fine, and it doesn't look like there is an intended specific path to the solution -- and that solution path is what makes a "puzzle" different from a "challenge", in my eyes.
– Deusovi
Feb 15, 2020 at 20:55
• There is a previous question - What is the strategy to solve Simon Tatham's Twiddle? with a general solution. Feb 16, 2020 at 8:08

Playing with this in Simon Tatham's Twiddle, I quickly found a solution in

12 moves

One such sequence is

A different (more elegant?) solution:

The notation shouldn't be difficult to understand.

AB
CD

This is not necessarily the shortest solution.

With the aide of computer:

10 moves minimum: AAB'DB'CCA'BA'

• Is the notation rotational or by row?
– JMP
Feb 16, 2020 at 6:34
• @JMP A is top-left, B is top-right, C is bottom-left and D is bottom-right. The apostrophe indicates CCW. Feb 16, 2020 at 6:38
• Thanks for the cool link! Feb 16, 2020 at 6:41
• @DanielMathias I am also trying to solve the same problem for 4x4 and 5x5 squares. I would be quite interested to see what your method can get on those. Feb 23, 2020 at 11:12
• @Dmitry My method of enumeration (brute force) is not suitable for larger grids. Feb 23, 2020 at 19:40

14

Notice that

The initial configuration of numbers is rotated 180 degrees from the desired final configuration.

Also notice that

the number 5 has to end up where it started from. Also, every rotation must involve the central number. So, each move of 5 away from the center must be matched by a move back. Any correct answer will be even.

The choice of starting rotation is arbitrary, but the rest of the moves will follow from that first choice. I started with rotating the lower left quartet clockwise.

9 8 7
3 6 4
2 5 1

(The highlighting shows the numbers that just moved.)

You then proceed

by advancing around the square in a counterclockwise fashion. If your first move had rotated a quartet counterclockwise, you would proceed clockwise.

The next couple of moves are

9 8 7   9 5 8   3 9 8
3 5 6   3 6 7   6 5 7
2 1 4   2 1 4   2 1 4

Some more moves:

3 9 8   3 9 8   3 5 9   2 3 9
2 6 7   2 5 6   2 6 8   6 5 8
1 5 4   1 4 7   1 4 7   1 4 7

More moves:

2 3 9   2 3 9   2 5 3   1 2 3
1 6 8   1 5 6   1 6 9   6 5 9
4 5 7   4 7 8   4 7 8   4 7 8 

And then:

1 2 3   1 2 3
4 6 9   4 5 6
7 5 8   7 8 9

What happens is

each number creeps around the edge towards its final destination. Some are moved into the center, but moved back out again by the next move.

No computers were consulted.

$$15$$ (if you count CC as a single move). First put the $$7$$ in it's correct place, and align and position the $$8$$ and $$9$$:

 987   945   945   945   945   235   235
654   678   681   361   328   498   461
321   321   372   782   716   716   789
The $$1$$ and $$4$$ are manoeuvred into place, and the rest follows:

 425   413   153   132
631   652   462   456
789   789   789   789
And following @Magma's comment:

 132   412   412   152   123
469   639   653   463   456
758   758   789   789   789
Trying @Magma's comment from the first $$789$$ doesn't do anything:
 235   425   425   165   165   635   623   163
461   631   619   249   234   124   145   425
789   789   738   738   789   789   789   789

• Rotate bottom right CCW, top left CW, bottom right CW, top left CCW, then top right CCW. This swaps the top right and top middle cells. Feb 16, 2020 at 0:53
• @Magma; thanks - I didn't know that was possible!. Do you know why my second part doesn't work?
– JMP
Feb 16, 2020 at 5:02
• You're rotating twice in the third step? Feb 16, 2020 at 11:16

I don't normally post answers to my own questions, but I found a nice solution that improves on the best score. I can do it in

10 moves!

Here is the method:

Each move is described with 3 values: row column direction. Row and column are 0-based indices of the top-left cell that we are rotating. CW is clockwise and CCW is counter-clockwise. Here is the solution

Original grid
987
654
321

move 1: 1 1 CCW
987
641
352

move 2: 1 0 CCW
987
451
632

move 3: 1 1 CCW
987
412
653

move 4: 1 0 CCW
987
152
463

move 5: 0 0 CW
197
582
463

move 6: 0 1 CCW
172
598
463

move 7: 1 1 CCW
172
583
496

move 8: 0 1 CCW
123
578
496

move 9: 1 1 CCW
123
586
479

move 10: 1 0 CW
123
456
789

I used a hill-climbing algorithm to find this. I'll describe it later if I have time.

• I edited my answer five days ago... Feb 21, 2020 at 14:15
• I see now. Didn't notice it before. Feb 21, 2020 at 21:27