The following provides a sequence but not necesarily
the shortest sequence.   I note that some
permutations have no solution (are not in the
subgroup generated by p).

p=: 3 2 1 0 4 5, 0 4 3 2 1 5,:0 1 5 4 3 2

tab=: 3 : 0
 'p q'=. y
 p=. p , ,/ {"1/~ p
 q=. q , , ,&.>/~ q
 b=. ~: p
 (b#p) ; < b#q
)

'P Q'=: tab^:_ p;<,&.>0 1 2

solve0=: Q {::~ P i. ]
solve =: solve0 @: <:

   solve 1 3 2 6 5 4
1 2 1
   solve 5 6 2 1 4 3
0 2
   solve 6 5 4 1 2 3
0 1 2



----- Original Message -----
From: Oleg Kobchenko <[EMAIL PROTECTED]>
Date: Monday, June 11, 2007 6:55 pm
Subject: [Jprogramming] 1D Rubik's Cube

> I found an interesting game, as found on Andrew Nikitin's 
> MSX-BASIC page http://nsg.upor.net/msx/basic/basic.htm ,
> and I am not sure if its solver has been given as a puzzle.
> Here it goes.
> 
> 1D Rubik's Cube is a line of 6 numbers with
> original position:
> 
>  1 2 3 4 5 6
> 
> which can be rotated in 3 different ways
> in groups of four:
>    _______                _______
>   (1 2 3 4)5 6  --(0)->  (4 3 2 1)5 6
>      _______                _______
>    1(2 3 4 5)6  --(1)->   1(5 4 3 2)6
>        _______                _______
>    1 2(3 4 5 6) --(2)->   1 2(6 5 4 3)
> 
> Given a scrambled line, return the shortest sequence of 
> rotations to restore the original position.
> 
> Examples:
> 
>   solve 1 3 2 6 5 4
> 1 2 1
>   solve 5 6 2 1 4 3
> 0 2
>   solve 6 5 4 1 2 3
> 0 1 2
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to