• 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

qqwref

Member
Joined
Dec 18, 2007
Messages
7,834
Location
a <script> tag near you
WCA
2006GOTT01
YouTube
Visit Channel
411 byte random-state scrambler for the 3x3x3. Probably can be golfed down a bit but at least this is kind of readable.
Code:
r=Math.random
f=(b,a)=>{document.write((v=[[,"L2UL2","","L2U'L2","UFU'","U'B'U","R'","R","F'RF","R2","BR'B'","D2R2D2"],[,"R'","","F","F2","R","R2","D'R2D"]][b][a])+[["L2UL2U'L2B2U'B2DL2D'","ULU'L'B'UBLF'LFL2"],["F2UF2UF2U'R'U'RF2R'UR","R'F2L2D'L'DL'F2RU'","LF2R'DR'D'R2F2L'U'"]][b][0|r()*(2+b)]+v+v+v)}
g=(b,a)=>{j=0|r()*(a+1);f(b,a);j==a?f(b,a):j?(f(b,j),f(b,a)):1}
for(i=7;i---1;)g(1,i)
for(i=11;i---1;)g(0,i)
It's a Fisher–Yates shuffle done with Classic Pochmann. The f function swaps some piece with the buffer (UL or ULB) using a random one of (2 T perms or 3 Y perms) to give it a random orientation. The setup moves are all of order 2 or 4, so I can just do them 3 times to undo them. The g functions and the for loops do the actual shuffle; for a swap, there are 3 possible cases. If we swap two non-buffer pieces x and y, do f(x)f(y)f(x). If we swap a non-buffer piece x with the buffer, do f(x). And if we swap the piece x with itself, since we still need to orient, we want to do f(x)f(x). The buffer is always piece 0 so we can end the loop early and never need to worry about swapping it with itself (which is good, because the buffer's orientation is already set based on the other pieces).

EDIT: ~680 move average out of 10 trials. The one I posted before that was based on doing cycles over and over had a ~1530 move average out of 10 trials.
 
Last edited:

Stefan

Member
Joined
May 7, 2006
Messages
7,280
WCA
2003POCH01
YouTube
Visit Channel
Quite clever, using setups with orders 2 and 4.

You could save a few bytes like this:
Code:
"L2UL2  L2U'L2 UFU' U'B'U R' R F'RF R2 BR'B' D2R2D2".split(" ")
(I just don't know why you have that preceding comma and what to do about that)

And you could use the "T perms" UFU'DFUD'FU'F2 and UFU'FUL'FLU'F2 (these swap DL with UR/RU) and use these setups:
Code:
" U U2 U' R R2 R' F'RF FU'F' BR'B' B'UB".split(" ")
And the same for corners.

At least for 2x2, I *think* you could use the "Y perm" RF2U'F'R'F2DLF either 1, 3 or 5 times. That swaps ULB with some orientation of DFR, so you don't need three Y perms. I'm not sure that this doesn't create biases, though, and whether it could be used for 3x3 as well. Might depend on the actual implementation and at least right now, I'm not going to write one.
 
Last edited:

qqwref

Member
Joined
Dec 18, 2007
Messages
7,834
Location
a <script> tag near you
WCA
2006GOTT01
YouTube
Visit Channel
Quite clever, using setups with orders 2 and 4.
Thanks :)

You could save a few bytes like this:
Code:
"L2UL2  L2U'L2 UFU' U'B'U R' R F'RF R2 BR'B' D2R2D2".split(" ")
(I just don't know why you have that preceding comma and what to do about that)
The preceding comma actually says to leave an undefined value in the 0th element of the array (that value doesn't matter and it's shorter than ""). I like this idea though, or at least molarmanful's version.

And you could use the "T perms" UFU'DFUD'FU'F2 and UFU'FUL'FLU'F2 (these swap DL with UR/RU) and use these setups:
Code:
" U U2 U' R R2 R' F'RF FU'F' BR'B' B'UB".split(" ")
And the same for corners.
Huh? It's an interesting idea, but I think it'd mess up the corners in a way that's based on the edge permutation. Maybe that's OK since corners are already in a uniformly random position... but maybe if we do this in both ways, corners modifying edges and edges modifying corners, there will be some positions we can't get because (corner scramble + edge side effect, edge scramble + corner side effect) doesn't match that position for any possible two scrambles. I'll have to think about it.

At least for 2x2, I *think* you could use the "Y perm" RF2U'F'R'F2DLF either 1, 3 or 5 times. That swaps ULB with some orientation of DFR, so you don't need three Y perms. I'm not sure that this doesn't create biases, though, and whether it could be used for 3x3 as well. Might depend on the actual implementation and at least right now, I'm not going to write one.
I like the idea of using the same alg for all the cases, but I'm also not sure this will work. Since you need a twisted cycle (like the algs you posted) you have to twist some other piece, but the one that gets twisted will depend on the setup moves, so you might break the uniformly-random aspect. We could also write the Y perms as a random ["", "FR", "R'F'"] setup to a single Y perm alg, but I'm not sure if that's more efficient, since we'd have to do it for T perms as well.

You could also split using 0, for example:
Code:
"L2UL20L2U'L20UFU'0U'B'U0R'0R0F'RF0R20BR'B'0D2R2D2".split(0)
Cool idea.


Down to 382:
Code:
r=Math.random
f=(b,a)=>{document.write((v=["0L2UL200L2U'L20UFU'0U'B'U0R'0R0F'RF0R20BR'B'0D2R2D2","0R'00F0F20R0R20D'R2D"][b].split(0)[a])+["L2UL2U'L2B2U'B2DL2D'0ULU'L'B'UBLF'LFL2","F2UF2UF2U'R'U'RF2R'UR0R'F2L2D'L'DL'F2RU'0LF2R'DR'D'R2F2L'U'"][b].split(0)[0|r()*(2+b)]+v+v+v)}
g=(b,a)=>{j=0|r()*(a+1);f(b,a);j-a?j?(f(b,j),f(b,a)):1:f(b,a)}
for(i=7;i---1;)g(1,i)
for(i=11;i---1;)g(0,i)
 
Last edited:

Stefan

Member
Joined
May 7, 2006
Messages
7,280
WCA
2003POCH01
YouTube
Visit Channel
Thanks :)but maybe if we do this in both ways, corners modifying edges and edges modifying corners, there will be some positions we can't get because (corner scramble + edge side effect, edge scramble + corner side effect) doesn't match that position for any possible two scrambles. I'll have to think about it.
Oops, yes, doing it both ways might not work. Imagine a different puzzle where the "corners" are just the places/numbers 1 to 8 and the edges are just the places/numbers -1 to -8. And the "swap algorithm" swaps corners at places 1 and s and also swaps edges at places -1 and -s. Then the corner perm and edge perm will always be the same. Not sure how comparable that is, but it does show that one has to be careful.

But you can certainly use it for edges and then your current way for corners, right? And maybe for corners it might be beneficial to swap ULB with DFR orientations instead of URF orientations. Then all setups are just a single move. Not sure about the swap alg lengths, though.

I like the idea of using the same alg for all the cases, but I'm also not sure this will work. Since you need a twisted cycle (like the algs you posted) you have to twist some other piece, but the one that gets twisted will depend on the setup moves, so you might break the uniformly-random aspect.
Ah, now I remember why I did it for 2x2 and was unsure about making it work for 3x3 :). I was thinking of keeping the DLB corner fixed permutation-wise, and use it for the orientation side-effect. If you use setup moves ""/F/F2/F'/R/R2, then it's always that DLB corner that gets twisted.
 
Top