> Note also that Roger's solution provides answers of the same length
> in all cases other than 1 2 3 4 5 6.

I don't understand what you mean by that.
My "solve" produces the following answers (as
from my msg):

   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

In what way are 0 2 and 1 2 1 answers of the same
length?



----- Original Message -----
From: Raul Miller <[EMAIL PROTECTED]>
Date: Monday, June 11, 2007 9:15 pm
Subject: Re: [Jprogramming] 1D Rubik's Cube

> On 6/11/07, Oleg Kobchenko <[EMAIL PROTECTED]> wrote:
> > 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
> 
> Here's a brute force approach:
> 
> init=: 3 :0
>  n=.,:1 2 3 4 5 6
>  o=. 3 2 1 0 4 5,0 4 3 2 1 5,:0 1 5 4 3 2
>  p=. o (] ~.@, ,/@:({"1/))^:_ n
>  r=. a:#~#p
>  b=.1{.~#p
>  while.0 <#k=.I.-.b do.
>    j=. p i. o {"1/ b#p
>    i=. 0 1 2,~&.>~/r{~I.b
>    ok=.,j e.k
>    r=.(ok#&,i) (ok#&,j)} r
>    b=. 1 (,j)} b
>    assert. (-:~.) b#r
>  end.
>  solve=: r {::~ p i. ]
>  r
> )
> init''
> 
> 
> This could probably be made simpler, but I think it's fast enough.
> 
> Note also that Roger's solution provides answers of the same length
> in all cases other than 1 2 3 4 5 6.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to