The knight is a chess piece that, when placed on the o-marked square, can move to any of the x-marked squares (as long as they are inside the board):


Eight knights, numbered from 1 to 8, have been placed on a 3×3 board, leaving one single square empty ..

They can neither attack each other, nor share the same square, nor leave the board: the only valid moves are jumps to the empty square.

Compute the minimum number of valid moves required to reach the following ordered configuration by any sequence of valid moves:


Output -1 if it is not reachable.

Example detailed: Possible in 3 moves

128       12.       123       123
356  -->  356  -->  .56  -->  456
7.4       784       784       78.

Input & Output

  • You are given three lines of three characters (containing each of the characters 1-8 and . exactly once)
  • You are to output a single integer corresponding to the smallest number of moves needed to reach the ordered configuration, or -1 if it is not reachable.
  • You are allowed to take in the input as a matrix or array/list
  • You are allowed to use 0 or in the input instead of .

Test cases






This is code-golf, so shortest code wins!

Credits to this puzzle

  • 1
    \$\begingroup\$ Can we replace . with something like 0 in the input? \$\endgroup\$
    – Aiden Chow
    Feb 17, 2022 at 8:27

7 Answers 7


Charcoal, 54 52 bytes


Try it online! Link is to verbose version of code. Save 4 bytes if printing any negative number is acceptable for an unreachable configuration. Explanation:


Input the grid and join the lines together into a single string.


Reorder the digits from the string starting at the original bottom right corner and taking clockwise knight's moves.


Output the difference from 28 of the absolute difference with 28 (or -1 if that is negative) of...


... finding the position of that string excluding the . in the compressed string 4381672438167, multiplying that by 8, and adding on the position of the ..


JavaScript (Node.js), 331 bytes


Try it online!

Takes a 9-character string where the dot is replaced by a zero.


As the trick is really fun to find, I've hidden it behind a spoiler.

There are only two spaces that are a knight's move away from the zero. Let us consider one of the pieces. If we move it to the zero, then we can decide to move it back to where it was. However, that wastes moves. Since there are only two spaces that are a knight's move away from any given space, this means that only one other piece can be moved to the new position of the zero, other than the one we just moved. As a result, we must move that piece. Using the same reasoning as above, this means that there is only one "path" originating from a given move, because we only have one choice at each stage. Eventually, we will end up at the ideal arrangement. Now, to account for the impossible cases, notice that there are only so many possible moves. Eventually, we will reach an arrangement that we have already seen. If we do, that means the case is impossible and we can break out of the loop and return -1.

A problem I have is that this is way too long, so I'll try to keep golfing it.

Fixed a bug that would create outputs of Infinity for certain inputs.


05AB1E, 40 39 38 bytes

-1 byte porting @Neil's 54 bytes Charcoal approach:


Try it online or verify all test cases.

Original answer:


Try it online or verify all test cases.

Both programs take a flattened list of digits as input with " " as empty square.


•4Gв©Tв•        # Push compressed integer 4381672438167
 8×             # Repeat it 8 times as string
   I            # Push the input-list
    •55˜u•      # Push compressed integer 83270561
     S          # Convert it to a list of digits
      è         # Index each into the input
       D        # Duplicate it
        ðk      # Get the index of the space " "
          U     # Pop and store this index in variable `X`
        þ       # Remove the space by only keeping digits
         J      # Join it together to a string
          k     # Get the first 0-based index of this in the large string
           8*   # Multiply this by 8
             X+ # Add index `X`
D               # Duplicate it
 56α            # Get the absolute difference with 56
    ‚           # Pair both together
     ß          # Pop and push the minimum
      ®         # Push -1
       M        # Push the largest number on the stack
                # (after which it is output implicitly as result)
•þÜε•           # Push compressed integer 16507238
 S              # Convert it to a list of digits
  D             # Duplicate it
   I            # Push the input-list
    ðk          # Get the index of the space " "
      k         # Use that to index into the duplicated list of digits
       ._       # Rotate the list of digits that many times towards the left
         Tи     # Repeat it 10 times as list
ü2              # Get all overlapping pairs of this list
  v             # Loop over each pair `y`:
   Ð            #  Triplicate the current list
                #  (which will be the implicit input in the first iteration)
    {           #  Sort the top copy
                #  (where the space will go to the end of the sorted digits)
     Qi         #  If the two lists are still the same
       N        #   Push the loop index
        D56α‚ß  #   Same as above
              q #   Stop the program
                #   (after which the result is output implicitly)
       }        #  Close the if-statement
        DyèyRǝ  #  Swap the values at indices `y`:
        D       #   Duplicate the list once more
         yè     #   Get the values at the `y` indices
           yR   #   Push the reversed pair `y`
             ǝ  #   Insert the values back into the list at those indices
}               # Close the loop
 ®              # Push -1
                # (which is output implicitly if we haven't encountered the `q`)

See this 05AB1E tip of mine (section How to compress large integers?) to understand why •4Gв©Tв• is 4381672438167; •55˜u• is 83270561; and •þÜε• is 16507238.

  • \$\begingroup\$ I think D56α‚ß can be 28α28α for the same byte count, but maybe you can deduplicate that somehow? \$\endgroup\$
    – Neil
    Feb 18, 2022 at 8:29
  • \$\begingroup\$ @Neil I'm afraid not. I could do it like 28©α®α or 2F28α}, but both would be the same byte-count. \$\endgroup\$ Feb 18, 2022 at 8:33
  • \$\begingroup\$ Oh well. It saved me two bytes in Charcoal, so I thought I'd mention it just in case. \$\endgroup\$
    – Neil
    Feb 18, 2022 at 8:41

Python 3, 126 bytes

def f(a,s=[*range(1,9),0],m=0,n=8):
 for o in(3,2,7,0,5,6,1,8)*7:m+=a!=s or m-55;s[n],s[o],n=s[o],0,o
 return min(56-m or-1,m)

Try it online!

-1 byte thanks to Jonathan Allan

-3 bytes thanks to Arnauld

There are 56 possible permutations. From the starting position, we can walk a circular path 7 times, until we reach the starting position again. If the input is found, we return the number of steps m required. If the path is shorter the other way around, we return 56-m.

  • \$\begingroup\$ Does it really always work? For instance, [1,0,3,4,5,6,7,8,2] should return 1. \$\endgroup\$
    – Arnauld
    Feb 17, 2022 at 13:00
  • \$\begingroup\$ def f(a,s=[*range(1,9),0],p=(3,2,7,0,5,6,1,8)*7,m=0): saves one. \$\endgroup\$ Feb 17, 2022 at 13:58

Excel, 158 bytes


Link to Spreadsheet


JavaScript (ES6),  110 108  105 bytes

This is very similar to Jitse's answer.

Expects a comma-separated string.


Try it online!


Ruby, 101 98 bytes


Try it online!

Fairly straightforward. Cycle generates all 56 possible positions and compares with the input.

Saved some bytes off the orignal version by counting from the solved position at -28 through 0 to +27, and using 28-z.abs to decode the output.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.