Create a program or function to unjumble a square of digits by flipping (reversing around the centre point) only rows and columns.
Input
Input will be a 9x9 grid of digits in the form of a 9 line string like the following:
986553229
264564891
759176443
643982153
567891234
526917874
685328912
891732537
117644378
This input format is non-negotiable - any solutions which are "creative" with the input format will be considered invalid.
Output
Output should be a list of flip moves which, when applied to the input in the given order, should recreate the target grid.
An example output (not a solution to the previous input example):
28IF5D3EAB9G3
This output format is also non-negotiable. There should be no newlines or spaces in the output, only the characters 1
-9
and A
-I
(lower case characters are acceptable in place of upper case characters if you prefer).
The target grid (the state you need to recreate) is as follows:
123456789
234567891
345678912
456789123
567891234
678912345
789123456
891234567
912345678
The numbers 1
-9
should be used as instructions to flip the rows, and the letters A
-I
should be used for the columns. This is shown below with the grid in its restored state.
ABCDEFGHI
|||||||||
vvvvvvvvv
1 -> 123456789
2 -> 234567891
3 -> 345678912
4 -> 456789123
5 -> 567891234
6 -> 678912345
7 -> 789123456
8 -> 891234567
9 -> 912345678
So an 8
means flip the second row from the bottom, and an F
means flip the sixth column.
In the case that no solution is possible, the program should end without outputting anything at all.
Examples
Input:
987654321
234567891
345678912
456789123
567891234
678912345
789123456
891234567
912345678
Output:
1
In this case only the top row needs flipping to return to the goal state.
Input:
123456788
234567897
345678916
456789125
567891234
678912343
789123452
891234561
912345679
Output:
I
In this case only the final column (column I
) needs flipping to recreate the goal state.
Input:
123456788
798765432
345678916
456789125
567891234
678912343
789123452
891234561
912345679
Output:
2I
In this case we need to flip row 2
and then flip column I
to return to the goal state.
Notes:
- Please include an example usage in your answer.
- The output given does not have to be the shortest sequence that will return the goal state - any sequence which returns the goal state will do as long as it works (i.e. as long as I can test it)
- I'll endeavour to test each answer and upvote all those that work and have obviously had an attempt at golfing.
- This is an open-ended competition - I'll accept the shortest answer sometime next week, but if a newer valid answer comes along which is shorter at any point in the future I'll change the accepted answer to reflect that.
The bounty has been set at 200 reputation for the shortest answer received by 23:59:59 (GMT) on 26/01/2014The bounty was awarded to Howard for his 268 character GolfScript solution.
Testing
Please provide your program's output for the following three test grids with your answer:
986553229
264564891
759176443
643982153
567891234
526917874
685328912
891732537
117644378
927354389
194762537
319673942
351982676
567891234
523719844
755128486
268534198
812546671
813654789
738762162
344871987
341989324
567891234
576217856
619623552
194435598
926543271
I've created a small Python program to generate valid grids for testing purposes:
import random
def output(array):
print '\n'.join([''.join(row) for row in array])
def fliprow(rownum, array):
return [row[::1-2*(rownum==idx)] for idx,row in enumerate(array)]
def flipcol(colnum, array):
return zip(*fliprow(colnum, zip(*array)))
def randomflip(array):
op=random.randint(0,1)
row=random.randint(0,9)
if(op==1):
return fliprow(row, array)
else:
return flipcol(row, array)
def jumble(array):
arraycopy=array
for i in range(10, 1000):
arraycopy=randomflip(arraycopy)
return arraycopy
startarray=[
['1','2','3','4','5','6','7','8','9'],
['2','3','4','5','6','7','8','9','1'],
['3','4','5','6','7','8','9','1','2'],
['4','5','6','7','8','9','1','2','3'],
['5','6','7','8','9','1','2','3','4'],
['6','7','8','9','1','2','3','4','5'],
['7','8','9','1','2','3','4','5','6'],
['8','9','1','2','3','4','5','6','7'],
['9','1','2','3','4','5','6','7','8']]
print output(jumble(startarray))
(-2, 4)
,(2, 4)
,(2, -4)
, or(-2, -4)
. \$\endgroup\$A1
) and move it toB1
. You can get a1
to the positionB1
, but it'll be the tile fromB9
, not the tile fromA1
. Because we're only allowed whole row/column flips, the upperleftmost 1 will only ever be in one of the four outermost corners. If I've mistaken the rules, please let me know. \$\endgroup\$