# 283 bytes Pyraminx random state optimal scrambler



## Stefan (Dec 11, 2010)

As suggested by Michael, here's a short random state optimal scrambler for Pyraminx, 283 bytes if written on one line (but I don't like "lines" wider than 80):


```
@q=A..X;for($a{"@q"}='';@[email protected][email protected],0,24;){for$m(UQMNHACDP,LPDEFTVWO
,ROWXSJKMQ,BTFGAHIJS){@m=map{-65+ord}split//,$m.$m;$m=~/./;for('',"'",3
){@n[@m[0..8]][email protected][@m[3..11]];$a{"@n"}//=([email protected],@n)&&$a{"@o"}." $&$_"}}
}@a=values%a;print$a[[email protected]],map{rand 3<2?" $_"."'"x rand 2:''}u,l,r,b
```

The variable names mean:
q: queue
a: algorithm
o: old state
n: new state
m: move

The sticker positions are named like this:


And here's a more readable version which also shows the progress and total time (admittedly it's slow, preparation takes about three minutes on my laptop) and stores the scramble algorithms in a file (this can be used to test correctness after code changes):


```
use Time::HiRes;
my $start = [ Time::HiRes::gettimeofday( ) ];

#--------------------------------------------------------
@q = A..X;
$a{"@q"} = '';
while ( @n = @o = splice @q, 0, 24 ) {
  printf "%6.2f%%\n", $ctr/9331.20 if ++$ctr%1280==0;
  for $m ( UQMNHACDP, LPDEFTVWO, ROWXSJKMQ, BTFGAHIJS ) {
    @m = map { -65+ord } split //, $m.$m;
    $m =~ /./;
    for ( '', "'", 3 ) {
      @n[@m[0..8]] = @n[@m[3..11]];
      $a{"@n"} //= (push @q, @n)
                && $a{"@o"}." $&$_"
    }
  }
}
@a = values %a;
print $a[[email protected]],
      map {rand 3<2?" $_"."'"x rand 2:''} u,l,r,b;
#--------------------------------------------------------

my $elapsed = Time::HiRes::tv_interval( $start );
print "\nElapsed time: $elapsed seconds!\n";
      
open OUT, ">algorithms.txt";
for $a ( sort @a ) {
  $a =~ s/^ *//;
  print OUT "$a\n";
}
close OUT;
```

[SIZE=-2](back to UWR now...)[/SIZE]


----------



## joey (Dec 11, 2010)

Any chance you can explain it?


----------



## Stefan (Dec 11, 2010)

Isn't it obvious? 

Straight-forward breadth-first search, mainly. Start with the solved state which is A..X and is generated with algorithm ''. Put it in a queue and then process the queue. Always take one state from the queue, apply all possible moves to it, and put the resulting states you haven't seen before into the queue and store their generating algorithms. At the end, pick one of the generating algorithms and add tip turns.

Or shorter: Compute all possible scrambles, then pick one.


----------



## Stefan (Dec 11, 2010)

28*2*:

```
@q=A..Y;for($a{"@q"}='';@[email protected][email protected],0,25;){for$m(UQMNHACDP,LPDEFTVWO
,ROWXSJKMQ,BTFGAHIJS){@m=map{64^ord}split//,$m.$m;$m=~/./;for('',"'",3
){@n[@m[0..8]][email protected][@m[3..11]];$a{"@n"}//=([email protected],@n)&&$a{"@o"}." $&$_"}}
}@a=values%a;print$a[[email protected]],map{rand 3<2?" $_"."'"x rand 2:''}u,l,r,b
```
Using 25 letters for the state (1-indexed rather than 0-indexed) allowed me to replace -65+ with 64^.


----------



## qqwref (Dec 11, 2010)

I guess I should convert this to javascript, huh?


----------

