14
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!
1By flipping 2 characters, did you mean flip from space to
*
and vice versa, or exchange them? – user202729 – 2018-04-08T14:57:41.850What if there are more than five
*
? Can you add some more test cases? – Stewie Griffin – 2018-04-08T14:58:18.273@user202729 for example abc would be acb if you flipped the last 2 characters. – Noah Cristino – 2018-04-08T15:08:09.503
@StewieGriffin "if it's not possible return a -1" more than 5
*
or less than 5 makes it impossible. – Noah Cristino – 2018-04-08T15:08:51.2636May we use something other than
-1
? For example5
(impossible otherwise), or throwing an error? – Jonathan Allan – 2018-04-08T18:38:38.840Could you include a test case where there are more than 5
*
s? – Post Rock Garf Hunter – 2018-04-09T21:20:48.173@JonathanAllan sure you can return any impossible input – Noah Cristino – 2018-04-09T23:55:21.453