So your task is to take a 3x3 block where -
's mean blank spaces, and *
's mean filled spaces, for example:
-**
-*-
*-*
and rearrange the block so that the *
's form an X, like this:
*-*
-*-
*-*
Input: 3x3 squares like above, they can be 3 lines, an array, or however you want.
Output: The shortest amount of moves to rearrange into an X. Each move is flipping 2 characters that are touching, and are horizontal from each other, vertical from each other, or diagonal from each other. If it's not possible, return any impossible output, for example 999
or -4242
. 5
is the smallest such number.
Test Cases:
1) Output: 1
-**
-*-
*-*
2) Output: -1
-*-
-*-
*-*
3) Output: 3
---
-**
***
4) Output: 0
*-*
-*-
*-*
You can substitute the blank and non blank characters but be sure to include which is which in your post
Code Golf
Remember this is code golf the shortest code wins!
*
and vice versa, or exchange them? \$\endgroup\$*
? Can you add some more test cases? \$\endgroup\$*
or less than 5 makes it impossible. \$\endgroup\$-1
? For example5
(impossible otherwise), or throwing an error? \$\endgroup\$