My python one-liner scramble generator

Alright, I think the normal 3x3 scrambler is getting too easy. Time to make it a little trickier. Here is a *pyraminx optimal scrambler* to try to code-golf. (Written in JavaScript of course.) It's kinda long, you might wanna copy it to Notepad or something before editing.

Code:
var j=1,b,mn=11,g=[],f=[],d=[],e=[],k=[],h=[],i=[];function u(){var c,p,q,l,m;for(p=0;p<720;p++){g[p]=-1;d[p]=[];for(m=0;m<4;m++)d[p][m]=w(p,m)}g[0]=0;for(l=0;l<=6;l++)for(p=0;p<720;p++)if(g[p]==l)for(m=0;m<4;m++){q=p;for(c=0;c<2;c++){q=d[q][m];if(g[q]==-1)g[q]=l+1}}for(p=0;p<2592;p++){f[p]=-1;e[p]=[];for(m=0;m<4;m++)e[p][m]=x(p,m)}f[0]=0;for(l=0;l<=5;l++)for(p=0;p<2592;p++)if(f[p]==l)for(m=0;m<4;m++){q=p;for(c=0;c<2;c++){q=e[q][m];if(f[q]==-1)f[q]=l+1}}for(c=0;c<j;c++){k=[];var t=0,s=0;q=0;h=[0,1,2,3,4,5];for(m=0;m<4;m++){p=m+n(6-m);l=h[m];h[m]=h[p];h[p]=l;if(m!=p)s++}if(s%2==1){l=h[4];h[4]=h[5];h[5]=l}s=0;i=[];for(m=0;m<5;m++){i[m]=n(2);s+=i[m]}i[5]=s%2;for(m=6;m<10;m++){i[m]=n(3)}for(m=0;m<6;m++){l=0;for(p=0;p<6;p++){if(h[p]==m)break;if(h[p]>m)l++}q=q*(6-m)+l}for(m=9;m>=6;m--)t=t*3+i[m];for(m=4;m>=0;m--)t=t*2+i[m];if(q!=0||t!=0)for(m=mn;m<99;m++)if(v(q,t,m,-1))break;b="";for(p=0;p<k.length;p++)b+="ULRB"[k[p]&7]+["","'"][(k[p]&8)/8]+" ";var a="lrbu";for(p=0;p<4;p++){q=n(3);if(q<2)b+=a[p]+["","'"][q]+" "}}}function v(q,t,l,c){if(l==0){if(q==0&&t==0)return true}else{if(g[q]>l||f[t]>l)return false;var p,s,a,m;for(m=0;m<4;m++)if(m!=c){p=q;s=t;for(a=0;a<2;a++){p=d[p][m];s=e[s][m];k[k.length]=m+8*a;if(v(p,s,l-1,m))return true;k.length--}}}return false}function w(p,m){var a,l,c,s=[],q=p;for(a=1;a<=6;a++){c=0|(q/a);l=q-a*c;q=c;for(c=a-1;c>=l;c--)s[c+1]=s[c];s[l]=6-a}if(m==0)y(s,0,3,1);if(m==1)y(s,1,5,2);if(m==2)y(s,0,2,4);if(m==3)y(s,3,4,5);q=0;for(a=0;a<6;a++){l=0;for(c=0;c<6;c++){if(s[c]==a)break;if(s[c]>a)l++}q=q*(6-a)+l}return q}function x(p,m){var a,l,c,t=0,s=[],q=p;for(a=0;a<=4;a++){s[a]=q&1;q>>=1;t^=s[a]}s[5]=t;for(a=6;a<=9;a++){c=0|(q/3);l=q-3*c;q=c;s[a]=l}if(m==0){s[6]++;if(s[6]==3)s[6]=0;y(s,0,3,1);s[1]^=1;s[3]^=1}if(m==1){s[7]++;if(s[7]==3)s[7]=0;y(s,1,5,2);s[2]^=1;s[5]^=1}if(m==2){s[8]++;if(s[8]==3)s[8]=0;y(s,0,2,4);s[0]^=1;s[2]^=1}if(m==3){s[9]++;if(s[9]==3)s[9]=0;y(s,3,4,5);s[3]^=1;s[4]^=1}q=0;for(a=9;a>=6;a--)q=q*3+s[a];for(a=4;a>=0;a--)q=q*2+s[a];return q}function y(p,a,c,t){var s=p[a];p[a]=p[c];p[c]=p[t];p[t]=s}function n(c){return 0|Math.random()*c}u();document.write(b);

:D
Optimizing that could be total madness (I've counted 34 for cycles nested in various ways) ... but it could also be fun.
The code right now is 2119 characters.
Before we try,
could you explain us how an optimal scrambler for pyraminx works
and how this specfic scrambler works.
In particular: what is ther role of the functions u, v, w, x ?

Or, for those ,like me, who don't know how it works:
we may try to optimize it just by looking at the code ...

Finally, if we ever optimize it, how many characteres do you think it could reach ?
1200 ? 1000 ? less ?
 
Last edited:
Perl: 100 chars
Code:
map{$m=0|rand 6until$c+$b-5|$c-$m&&$m ne$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25

I can't find any bugs in this code..

(sorry for bumping, but 100 chars is better than the previous 102 chars PERL :P)
 
Recap 4

Recap 0.
Recap 1.
Recap 2.
Recap 3.
Recap 4:


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


C++: 102 (110 with main(){} ) (keemy, Marcell, guzman, Portponky, Stefan)
Code:
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;

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

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

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}*" "

php: 114 (Marcell, guzman)
Code:
for($i=$b=$c=25;$i;$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: 139 (masterofthebass, Odder) (Edited)
Code:
from random import randint as r
m=b=9
for u in range(25):
 c=b;b=m
 while c+b-4 and m==c or m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],

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})]

Visual Basic: 170 (mr. giggums)
Code:
For s = 1 To 25
c = b
b = a
Do While a = b Or a + b = 7 And a = c
a = Int(Rnd * 6 + 1)
Loop
t.Text = t.Text & Mid("URFBLD", a, 1) & Mid(" '2", Int(Rnd * 3 + 1), 1)
Next s
 
Last edited:
You forgot my Python(140 chars):
Code:
from random import randint as r
c=m=b=9
for u in range(25):
 while c+b-4 and m==c or m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],;c=b;b=m

it is based on the code from masterofthebass' code

EDIT:
saved 1 char :P (and should still be working ;)
Code:
from random import randint as r
m=b=9
for u in range(25):
 c=b;b=m
 while c+b-4 and m==c or m==b:m=r(0,5)
 print "URFBLD"[m]+" '2"[r(0,2)],
 
Last edited:
That's the same I posted earlier, and like I said before, this does not create scrambles starting with DU.

My fault :S (I didn't see your bugged 100 char solution) but after reading my own code it's pretty clear that it can never start with DU, so 102 is definitely the shortest yet =)
 
Before we try,
could you explain us how an optimal scrambler for pyraminx works
and how this specfic scrambler works.
In particular: what is ther role of the functions u, v, w, x ?
I don't really know the details. The general way it works is to set up some permutation/orientation tables (which I guess explain to the program what each move does) and then does a search, starting at 1 move and going up, to see if any sequences of that length solve the position. The "u();document.write(b);" part calls what is necessary for the scrambler.

Finally, if we ever optimize it, how many characteres do you think it could reach ?
1200 ? 1000 ? less ?
Well, it's already condensed and optimized a bit (see the variable names etc.) so I doubt you'll get a huge amount out of it, maybe down to 1500 or so if the approach doesn't change.
 
Ah, ok.
But I can win few chars, if instead of randint I import randrange.
It takes 1 argument instead of 2:

from random import randrange as r
m=b=9
for u in range(25):
c=b;b=m
while c+b-4 and m==c or m==b:m=r(5)
print "URFBLD"[m]+" '2"[r(2)],

This is 2 chars less.
 
If I do that on C++, does the count have to include the includes and the "void main" or only the line?

The line is as follows:
for(int i=25;i;i--)printf("%c%c ","UDFBRL"[rand()%6]," 2'"[rand()%3]);

(70 chars)

output :
B' R2 B2 D2 L2 U' F' U R2 F D2 F R' F2 D' F2 L U' L2 U2 F' B U D2 L
 
Last edited:
Someone should start an official scrambler programming contest.

Here's my pathetic attempt in Ruby:


m= ["R", "U", "D", "B", "F", "L"]
x = [" ", "2 ", "' "]
o = 0
d = 0
while d < 26
n = m.at(rand(6))
if n != o
print n
o = n
print x.at(rand(3))
d = d + 1
end
end


148 characters.
 
Last edited by a moderator:
Back
Top