Solving the “Rotation” puzzle in stages


A few months ago (in June last year), I wrote a simple puzzle named “Rotation”  (play here) that involves rearranging a grid of jumbled-up numbers in order by rotating groups of numbers about.

Jumbled-up grid

Jumbled-up grid

The player clicks on the cyan colored buttons in order to rotate the group of numbers immediately adjacent to it clockwise. In the above example, rotation C rotates 5, 8, 4, 3 clockwise such that 5 is now where 8 was, and so on. The goal of the game is to rotate the numbers about until all the numbers on the grid are eventually in the correct order:

Ordered grid

Ordered grid

In this article, we develop an AI that solves the puzzle using a sequence of 8 stages. The AI places the numbers into their correct positions one by one at each stage. The possible number of game states reduces after each stage until there is only one possible game state left, and the puzzle is solved.

The stages, in sequence order

As mentioned earlier, the strategy we will be employing is to move the numbers into their correct positions one by one, without affecting the positions of the numbers corrected in the preceding stages. The top row is completed first, followed by the bottom row, and finally the middle row. The exact order of correcting the numbers is


Note that despite having 9 numbers, there are only 8 stages instead of 9 stages. This is because placing the preceding 8 numbers in their correct position leaves only one possible position for the last number – which is its own intended position.

The decision matrix

At each stage, the corresponding number m is moved to its correct position using a sequence of rotations depending on the current position of m. We define the decision matrix D(m) with entries such that if the number m is currently at row r and column c, the rotation(s) executed by the AI will be D(m)_{r,c}. This will be repeated until the number m is at its intended position.

We will use the first stage as an example. The first stage moves the number 1 to the top left corner. The corresponding decision matrix D(1) is given by

D(1) =\left(\begin{matrix}I&A&B\\A&A&B\\C&C&D\end{matrix}\right)

Suppose the number 1 is currently at the bottom left corner (row 3, column 1). Then, using the decision matrix, rotation C will be executed, moving 1 to the leftmost corner of the middle row. Since 1 is still away from its correct position, the decision matrix will be applied again – rotation A will consequently be executed, shifting 1 to its correct position.

Note that D(1)_{1, 1} = I, the identity element – this means that if the intended number 1 is already at its intended position, then no rotation will be made and the AI will proceed immediately to the next stage, described by D(2).

Having moved the intended number to its intended position, the AI moves on to the next stage. The stages are completed sequentially in this manner until the puzzle is eventually solved.

Exact decision matrices

The decision matrices used by the AI here at each of the stages is given exactly in order below. As the stages are to be executed sequentially, there are some positions at each stage where the intended number cannot be, as the correct numbers are already moved to these position in the preceding stages. These illegal positions are marked with the element 0 in the decision matrices.

Note also that for better intuition, anticlockwise rotations are denoted by X^{-1} instead of X^3. Of course, the rules of the game are such that only clockwise rotation are allowed. As such, when we encounter X^{-1}, we will execute it as X^3.

D(1) =\left(\begin{matrix}I & A & B \\ A & A & B \\ C & C & D \end{matrix}\right)

D(2) =\left(\begin{matrix}0 & I & B \\ C & B & B \\ C & D & D \end{matrix}\right)

D(3) =\left(\begin{matrix}0 & 0 & I \\ C & D & D \\ C & B^2C^{-1}B^2 & D \end{matrix}\right)

D(7) =\left(\begin{matrix}0 & 0 & 0 \\ C & C & D \\ I & C & D \end{matrix}\right)

D(8)=\left(\begin{matrix}0 & 0 & 0 \\ CDC^{-1} & D & D \\ 0 & I & D\end{matrix}\right)

D(9) =\left(\begin{matrix}0 & 0 & 0 \\ CD^2C^{-1} & CD^{-1}C^{-1} & CDC^{-1} \\ 0 & 0 & I \end{matrix}\right)

D(5) =\left(\begin{matrix}0 & 0 & 0 \\ CBC^{-1}B^{-1} & I & BCB^{-1}C^{-1} \\ 0 & 0 & 0 \end{matrix}\right)

D(4) =\left(\begin{matrix}0 & 0 & 0 \\ I & 0 & C^2BCB^{-1}CBCB^{-1} \\ 0 & 0 & 0 \end{matrix}\right)

Implementation and analysis

The AI described here is implemented alongside the game itself (link).

From the decision matrices, the maximum number of rotations needed at each step sum up to

4 + 3 + 10 + 3 + 5 + 7 + 8 + 13 = \mathbf{53}

clockwise rotations. That is, it takes at most 53 moves to complete the puzzle.

The number of cases considered by the AI at each stage sum up to

9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 = \mathbf{44}

In contrast to the brute force method of coming up with a solution sequence for each of the 9!=362880 possible puzzles, the small number of cases to be considered requires a lot less memory space. However, more moves are required because the game states can sometimes be solved by shorter rotation sequences, but these shorter sequences are not identified by the AI.


While it is possible to come up with faster strategies which consider more cases, the aim of this article is to allow the reader to have a general idea of how to solve “Rotation” – and similar puzzles – using the approach of breaking up a strategy into stages. As such, the strategy used in this article is simpler albeit slower, and the exploration of more complex strategies is left to the reader.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s