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

A really short Javascript scrambler function

Lucas Garron

Administrator
Joined
Jul 6, 2007
Messages
3,718
Location
California
WCA
2006GARR01
YouTube
Visit Channel
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
301
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
91
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
Visit Channel
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
91
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.
 

qqwref

Member
Joined
Dec 18, 2007
Messages
7,834
Location
a <script> tag near you
WCA
2006GOTT01
YouTube
Visit Channel
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