molarmanful
Member
I recently got into code-golfing, and I decided to JS-golf a 3x3 scrambler. The Gist is at https://gist.github.com/molarmanful/2f299ae96f6120cf5283. What do you guys think? Any ideas for saving more bytes?
I recently got into code-golfing, and I decided to JS-golf a 3x3 scrambler. The Gist is at https://gist.github.com/molarmanful/2f299ae96f6120cf5283. What do you guys think? Any ideas for saving more bytes?
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?
<script>r=Math.random;for(c=b=j=25;j;c+b-5|c-m&&b-m?document.write("URFBLD"[j--,c=b,b=m]+" 2'"[0|r()*3]+" "):0)m=0|r()*6</script>
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).
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(' ')}
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(' ')
}
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(' ')}()
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.
I've made an example page at this link if people want to test it out:
https://jsfiddle.net/sz8jw62t/2/
Code:<script>r=Math.random;for(c=b=j=25;j;c+b-5|c-m&&b-m?document.write("URFBLD"[j--,c=b,b=m]+" 2'"[0|r()*3]+" "):0)m=0|r()*6</script>
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.
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.
The set objective for this code golf is to output a scramble right?
<!DOCTYPEhtml><body><p>DRL2BD2F2R2B2F2D'UBD'F2UF'U'D'FDB'F2R2L'R'</P></body> (76 characters by my count)
No? Ok
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.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.
The set objective for this code golf is to output a scramble right?
<!DOCTYPEhtml><body><p>DRL2BD2F2R2B2F2D'UBD'F2UF'U'D'FDB'F2R2L'R'</P></body> (76 characters by my count)
No? Ok