• Welcome to the Speedsolving.com, home of the web's largest puzzle community!
    You are currently viewing our forum as a guest which gives you limited access to join discussions and access our other features.

    Registration is fast, simple and absolutely free so please, join our community of 40,000+ people from around the world today!

    If you are already a member, simply login to hide this message and begin participating in the community!

My python one-liner scramble generator

guzman

Member
Joined
Aug 19, 2010
Messages
91
Location
Firenze, Italy
Using the empty space of the 'for' we can gain one character more:
121 characters:

Code:
r=Math.random;for(c=b=j=0;j<25;c+b==5&&c==m||b==m||(c=b,b=m,j++,document.write("URFBLD"[m]+" 2'"[0|r()*3]+" ")))m=0|r()*6

It is interesting that the code is not so involved, it is still quite readable and linear in its structure.


EDIT: 115 characters
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]+" "))m=0|r()*6

(111 if we accept uneven spaces in the output).
 
Last edited:

Johannes91

Member
Joined
Mar 28, 2006
Messages
1,341
qqwref, guzman: Nice!

Shortened Patrick's Perl a bit, from 115 to 107:
Code:
map{$m=int 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
 

Marcell

Member
Joined
Sep 1, 2007
Messages
294
WCA
2007ENDR01
My try:
127 characters in php.
Code:
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b!=$m=rand(0,5))&($c+$b!=5||$c!=$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>

Edit: 123.
Code:
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b-$m=rand(0,5))&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
 
Last edited:

Marcell

Member
Joined
Sep 1, 2007
Messages
294
WCA
2007ENDR01
122 characters.
Code:
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b-$m=rand()%6)&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>

Also, here's Johannes' code implemented in php - ironically enough this one is 122 chars as well.
Code:
<?$M="URFBLD 2'";$b=$c=0;for($i=0;$i<25;$i++){while(!($b-$m=rand()%6&($c+$b-5|$c-$m)));echo$M[$b=$m].$M[rand(6,8)].' ';}?>
 

guzman

Member
Joined
Aug 19, 2010
Messages
91
Location
Firenze, Italy
122 characters.
Code:
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if(($b-$m=rand()%6)&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>

Also, here's Johannes' code implemented in php - ironically enough this one is 122 chars as well.
Code:
<?$M="URFBLD 2'";$b=$c=0;for($i=0;$i<25;$i++){while(!($b-$m=rand()%6&($c+$b-5|$c-$m)));echo$M[$b=$m].$M[rand(6,8)].' ';}?>

Nice.
I think there's place for improvement in your php code.
I dont have a php compiler installed but I think that you can use many of the ideas in our javascript code:
you can initiate b and c inside the for: for($b=$c=$i=0;....),
you can avoid the while by justing letting the for cicle until it finds a good case (and increase i only in that case).

You should get very close to 110.
 
Last edited:

Marcell

Member
Joined
Sep 1, 2007
Messages
294
WCA
2007ENDR01
Smart!
114 now:
Code:
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if($b-$m=rand()%6&($c+$b-5|$c-$m)){echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>

I'm afraid php doesn't let me leave out the "if" statement.
Also, I did initiate b and c in the for loop in my first code, but forgot to do so in the second. Thanks for pointing that out.
BTW I don't have a php compiler either, I let it run on a web server.
 
Last edited:

guzman

Member
Joined
Aug 19, 2010
Messages
91
Location
Firenze, Italy
Smart!
114 now:
Code:
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if($b-$m=rand()%6&($c+$b-5|$c-$m)){echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>

Some improvements:
112 considering tags or 108 not considering them (as in javascript):
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].$M[rand(6,8+0*$i++)].' '

Edit: but there was an error in your code and I copied it. So this isn't valid.
 
Last edited:

guzman

Member
Joined
Aug 19, 2010
Messages
91
Location
Firenze, Italy
You're right !!

my code also has the same error cause I started from yours and I didn't check,
but actually I don't see wrong scrambles ...

EDIT: wrong scrambles do appear.
 
Last edited:

Stefan

Member
Joined
May 7, 2006
Messages
7,280
WCA
2003POCH01
YouTube
Visit Channel
75 bytes:
print "F' D B2 L B R2 B2 L R' D' R' F L' R' U2 L B2 U2 D' F' R' U2 B L2 F'"
// chosen by fair dice rolls. guaranteed to be random.
 

Marcell

Member
Joined
Sep 1, 2007
Messages
294
WCA
2007ENDR01
@StachuK1992: Just like me.

@guzman: Oh well. 120 characters then. *sigh*
Code:
<?$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if($b-$m=rand()%6&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>

Edit: but I'm not sure whether I'm obliged to count the <? and ?> tags as well. I mean, you didn't count the <script></script> either.
Edit2: never mind. I didn't see your edited post. So I think it's fair to say that it's 116 characters long:
Code:
$M="URFBLD 2'";for($i=$b=$c=0;$i<25;)if($b-$m=rand()%6&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}

Edit3: however your idea about nesting the $M="..." into the for statement was actually valid. 115 characters:
Code:
for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if($b-$m=rand()%6&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}
 
Last edited:

Marcell

Member
Joined
Sep 1, 2007
Messages
294
WCA
2007ENDR01
Crap, I'm still getting wrong scrambles. No idea why.

Edit: I see. I lost track of those brackets. It should be fine now. But now it's 121.
Code:
<?for($i=$b=$c=0;$i<25;$M="URFBLD 2'")if(($b-$m=rand()%6)&($c+$b-5|$c-$m)){$c=$b;echo$M[$b=$m].$M[rand(6,8)].' ';$i++;}?>
 
Last edited:

guzman

Member
Joined
Aug 19, 2010
Messages
91
Location
Firenze, Italy
anyone care to explain what the general idea is behind a scramble program?

- the program has to give a sequence of 25 letters chosen randomly amogn RFULBD,
- each letter appears with an "exponent" chosen ramdomly among 1,2,3: that is, for instance R may appear as R, R2 or R',
- consecutive letters cannot be the same
- if R follows L, the next letter cannot be L again (and similarly for the other opposite letters in whatever form they appear).

that's it.
 
Top