My python one-liner scramble generator

A GAP random-state scramble generator(169 characters, only the last 37 characters needed for additional scrambles - the text in red is one-time initialization code):

G:=Group(U,D,L,R,F,B);;P:=FreeGroup("U","D","L","R","F","B");;h:=GroupHomomorphismByImages(P,G,GeneratorsOfGroup(P),[noparse][U,D,L,R,F,B]);;[/noparse]
PreImagesRepresentative(h,Random(G));

OK, I cheated a bit. The output doesn't look exactly what it should look like. I also omitted initialization of globals U,D,L,R,F, and B (which also only needs to be done once, of course). (Note: The "forum" put in a couple of unwanted space characters, should be fairly obvious.)
 
Last edited:
Recap 2:

Recap 2:

perl: 98 (edit, sub100!!) (Patrick, Johannes91, Stefan, qqwref)
Code:
map{$m=0|rand 6until$c+$b-5|$c-$m&&$m-$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25


C++: 103 (111 with main(){} ) (edit) (keemy, Marcell, guzman, Portponky)
Code:
for(int c=0,b=6,j=25,m;j;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ");

perl: 111 (Stefan)
Code:
$_.=chr rand 99,/[^URFBLD]|(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/&&chop until/.{25}/;s/./" $&".qw(' 2)[rand 3]/ge;print

javascript: 114 (qqwref, Devin, guzman, Johannes91, Patrick, Stefan)
Code:
r=Math.random;for(c=b=j=0;j<25;c+b-5|c-m&&b-m?document.write("URFBLD"[j++,c=b,b=m]+" 2'"[0|r()*3]+" "):0)m=0|r()*6

Ruby: 115 (joey)
Code:
p (x=y=0..25).map{z=x;x=(w=[%(UD),%(FB),%(RL)][rand 3])[rand 2] until x!=y||z!=x&&w[y];(y=x)+"2'"[rand 3].to_s}*" "

perl: 115 (Patrick, Stefan)
Code:
do{$_=join'',map{(U,R,F,B,L,D)[rand 6]}1..25}while/(.)\1|LRL|RLR|UDU|DUD|FBF|BFB/;print" $_".qw(' 2)[rand 3]for/./g

php: 116 (Marcell, guzman)
Code:
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m))echo$M[$b=$m+0*$i++*$c=$b].$M[rand(6,8)].' '

python: 149 (masterofthebass)
Code:
from random import randint as r;c=b=0
for i in range(25):
 m=r(0,5)
 while (c+b-4 or c==m)and m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m

mathematica: 166 (Lucas)
Code:
StringJoin[ToString[#]<>(c=RandomChoice)[{" ","' ","2 "}]&/@(Flatten[RandomSample[#,Ceiling[Random[]*5/4]]&/@c[{{U,R},{F,B},{L,D}},25]]//.{a___,b_,b_,c___}:>{a,b,c})]
 
Last edited:
I'd say you cheated a lot :| The permutations U,F,R,D,L,B take a significant amount of space to define. If you don't count variable initializations then I claim the following JS code:

Code:
for(;j<k;c+b-5|c-m&&b-m?w(x[j++,c=b,b=m]+y[s()]+z):0)m=t()

:)

Or, just

p()

where the initialization of p is omitted. :)
 
The c++ algorithm can't produce a U turn on its first move. Initialize b to something like 6 to allow this.

You're right, thanks, I'll update it.

Edit: same goes for javascript, php, python.
I think we should set b=c=j=6 and j<31.
 
Last edited:
Damn, the timezones...
Anyway, it's really impressive what you did to that Perl code. And congrats to the sub100.

If only I could understand what the regex one does...
 
Code:
for(int j=0;j<25;j++)
for(int j=25;j;j--)

2 character saving for c++

Code:
for(int c=0,b=6,j=25,m;j;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ");
 
This thread is actually starting to scare me a little. 98 characters to make a legitimate scramble? Incredible.
 
98 characters to make a legitimate scramble? Incredible.

And so far, not achieved.

Saving 1 by not wasting the loop body:
for(int c=0,b=6,j=25,m;j;m-c|c+b-5&&b-m&&cout<<"URFBLD"[j--,c=b,b=m]<<" 2'"[rand()%3]<<" ")m=rand()%6;

I guess another could be saved by making the variables global so we don't need the =0, but then we really ought to include and count "main...".
 
Last edited:
Back
Top