My python one-liner scramble generator

Recap

Recap. So far:
(I've included some of the variations that use different ideas):

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

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

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

javascript: 114
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
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
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
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)].' '

pyton: 149 (edit)
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
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:
:) Just fantasies: If we had rand(n) and print() in javascript we would reach 91:

Code:
for(c=b=j=0;j<25;c+b-5|c-m&&b-m?print("URFBLD"[j++,c=b,b=m]+" 2'"[rand(3)]+" "):0)m=rand(6)
 
Last edited:
just because the OP's program sucks (no offense), I did a very basic one in python :p

151 chars (including newlines)
Code:
from random import randint as r;c=b=0
for i in range(25):
 m=r(0,5)
 while not((c+b-5 or c-m)and m-b):m=r(0,5)
 print "URFBLD"[m]+"'2"[r(0,1)],;c=b;b=m
 
C++:

Code:
for(int c=b=j=0,m;j<25;(m=rand()%6)-c|c+b-5&&b-m&&cout<<"URFBLD"[j++,c=b,b=m]<<" 2'"[rand()%3]<<" ");

possible? makes it 101 chars.
 
I don't speak Python, but wouldn't this part cause the code to never produce clockwise turns?

you're right :/ I had some other "optimizations" that cut it down to 149 now:
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

the "problem" is that it prints 2 spaces after a clockwise turn.
 
Or 104 with the space before instead of after the turn:
map{$m=0|rand 6until($c+$b-5||$c-$m)&&$m-$b;print" ".(U,R,F,B,L,D)[$m].qw(' 2)[rand 3];$c=$b;$b=$m}1..25

101:
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
 
Last edited:
Nope:
int c=b=j=0

if that was allowed we would have done it.
You can't do that in C++,
you have to declare a variable before you use it.

You need a C++ compiler if you want to start experimenting.

I have Visual Studio already, I just wanted to know. >.>
 
EDIT: 100?
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

That seems to work, but if it does, I'm surprised.

Here's another way to reach 100:
map{$m=0|rand 6while$c+$b==5&$c==$m|$m==$b;$c=$b;print" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25
 
EDIT: 100?
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

ahhh, I was just about to post that. One more char to go!

That seems to work, but if it does, I'm surprised.

I tested it by doing this a few times:

Code:
for(1..10000){map{$m=0|rand 6until($c+$b-5|$c-$m)&&$m-$b;if ($c+$b==5&&$m==$c) {print "NOPE"}$c=$b;" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25}

So I guess it does.
 
Last edited:
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

Someone tell me why this is wrong. It couldn't have been that easy. It passes my testing(below), though. (98 chars).

Code:
for(1..10000){map{$m=0|rand 6until$c+$b-5|$c-$m&&$m-$b;if ($c+$b==5&&$c==$m||$m==$b) {print "NOPE"}$c=$b;" ".(U,R,F,B,L,D)[$b=$m].qw(' 2)[rand 3]}1..25}
 
Last edited:
Now prove that it works, I don't trust tests :p

If you compared the values, you'd have 0 or 1 which you could safely "|" and "&". But since you subtract, you just get integers, and I don't see why it works. For example, 2&&1 is true while 2&1 is false.
 
Back
Top