• 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 35,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!

A really short Javascript scrambler function

Lucas Garron

Member
Joined
Jul 6, 2007
Messages
3,557
Location
California
WCA
2006GARR01
YouTube
LucasGarron
Code golf is great fun! How many bytes would you need to *add* to make it be a proper random-state
scrambler with reasonably short result sequences?
This is actually a great question.

My first instinct is to decompose the cube into Thistlethwaite-like steps, and store a table to randomize each subgroup in order. Unfortunately, I'm not aware of a decomposition that's likely to lead to something short enough that a scrambler would be okay using it instead of our current high-quality scrambles (say, under 30 moves).
 

rokicki

Member
Joined
Oct 31, 2008
Messages
270
This is actually a great question.

My first instinct is to decompose the cube into Thistlethwaite-like steps, and store a table to randomize each subgroup in order. Unfortunately, I'm not aware of a decomposition that's likely to lead to something short enough that a scrambler would be okay using it instead of our current high-quality scrambles (say, under 30 moves).
Machines are fast enough these days so you can do a full two-phase solver
including generating a partial table for each phase and get results pretty
quickly. Quick enough? Maybe. Is a few seconds fast enough? You don't
really need that much of either table to do a reasonable job, as Kociemba
proved with his Atari ST solver.

And two-phase would simplify things over Thistlethwaite, as well as
guarantee good solutions.

You're still probably talking a kilobyte of compressed code, though.

Maybe I should start another "short cube solving contest" with slightly
different metrics to see what people can come up with.
 

Calode

Member
Joined
Apr 21, 2014
Messages
90
Location
Washington
WCA
2016HOOV01
I had a go at it, learned a few things from you. My attempt (129 bytes):
Code:
function(){a=l=[];r=Math.random;while(l++<25)a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);return a.join(' ')}
Or expanded:
Code:
function(){
    a=l=[];
    r=Math.random;
    while(l++<25)
        a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);
    return a.join(' ')
}
I love your clever use of taking advantage of the update part of the for loop to add onto the generated move. Instead, I took an approach where I remove the previous move of the possible moves to generate. I still feel like I could make it smaller.

edit: just realized my code for checking the previous move doesn't even work. I fixed it and now it's 132 characters:
Code:
 function(){a=p=l=[];r=Math.random;while(l++<25)a.push((p="RLUDFB".replace(p,'')[0|r()*5])+["'",2,''][0|r()*3]);return a.join(' ')}()
All I did was add p and set p to the previous move.
 
Last edited:

molarmanful

Member
Joined
Dec 13, 2014
Messages
393
Location
Smerbia
WCA
2015PANG02
YouTube
molarmanful
I had a go at it, learned a few things from you. My attempt (129 bytes):
Code:
function(){a=l=[];r=Math.random;while(l++<25)a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);return a.join(' ')}
Or expanded:
Code:
function(){
    a=l=[];
    r=Math.random;
    while(l++<25)
        a.push('RLUDFB'.replace(a[l-2],'')[0|r()*5]+["'",2,''][0|r()*3]);
    return a.join(' ')
}
I love your clever use of taking advantage of the update part of the for loop to add onto the generated move. Instead, I took an approach where I remove the previous move of the possible moves to generate. I still feel like I could make it smaller.

edit: just realized my code for checking the previous move doesn't even work. I fixed it and now it's 132 characters:
Code:
 function(){a=p=l=[];r=Math.random;while(l++<25)a.push((p="RLUDFB".replace(p,'')[0|r()*5])+["'",2,''][0|r()*3]);return a.join(' ')}()
All I did was add p and set p to the previous move.
Nice! Little tip: you can save even more bytes by using for instead of while loops.

As of this post, the byte count for my Gist is at 127.
 
Last edited:

Calode

Member
Joined
Apr 21, 2014
Messages
90
Location
Washington
WCA
2016HOOV01
I realized as I tried optimizing it more (getting rid of .push, now 129 bytes), that I keep getting closer to your code.
At first, I tried using Array.map and get rid of the loops but as I tried I found out that it was hard so I ended up using a while loop and it kept getting closer to yours.
 

biscuit

Member
Joined
Sep 8, 2014
Messages
1,814
Location
Kansas City
WCA
2015WEBS01
YouTube
channel/UCX5xp55Ms06qsUOckyrJSQA
I've made an example page at this link if people want to test it out:

https://jsfiddle.net/sz8jw62t/2/
pretty good but with your code it allows "useless" moves. Here is a scramble generated with your code. U R2 D' L' D' L R D L R2 F' U2 D B R' B2 F2 B2 F D2 F D2 R2 L' U2

As you can see there is a B2 F2 B2 which is a bad thing.
 

molarmanful

Member
Joined
Dec 13, 2014
Messages
393
Location
Smerbia
WCA
2015PANG02
YouTube
molarmanful
pretty good but with your code it allows "useless" moves. Here is a scramble generated with your code. U R2 D' L' D' L R D L R2 F' U2 D B R' B2 F2 B2 F D2 F D2 R2 L' U2

As you can see there is a B2 F2 B2 which is a bad thing.
Oh, I completely forgot to take that into account! Well, back to coding...
 

samuelqwe

Member
Joined
Mar 16, 2014
Messages
87
Location
Ottawa, Canada
WCA
2014DION01
YouTube
PuzzledSam
pretty good but with your code it allows "useless" moves. Here is a scramble generated with your code. U R2 D' L' D' L R D L R2 F' U2 D B R' B2 F2 B2 F D2 F D2 R2 L' U2

As you can see there is a B2 F2 B2 which is a bad thing.
I didn't make the scrambler, I just made an example out of the existing one found in this thread.
 

qqwref

Member
Joined
Dec 18, 2007
Messages
7,830
Location
a <script> tag near you
WCA
2006GOTT01
YouTube
qqwref2
Wait a sec... Not counting the script tags, you just wrote a 112-byte scrambler. Nice! I'll add this to the Gist in a separate file.
Oh, I didn't write that just now, that's a solution from the previous thread (from 2010). I have that entire thing saved as a working .htm file, and both Firefox and Chrome are nice enough to run it even without <html>/<head>/<body> tags.
 
Top