Skip to content

Instantly share code, notes, and snippets.

@molarmanful molarmanful/0.md
Last active Aug 29, 2015

Embed
What would you like to do?
Javascript-golfed Rubik's Cube scramblers.

Javascript-Golfed Rubik's Cube Scramblers

All scramblers here are split into 4 categories (puzzles of all types are welcome):

  1. Random moves
  2. Random moves, basic cancellation (no R' R, U U2, etc.)
  3. Random moves, extensive cancellation (no R L R, U D2 U2, etc.)
  4. Random state

Random moves scramblers for WCA puzzles must generate scrambles of lengths:

  • 2x2: 11
  • Pyraminx: 11
  • Skewb: 11
  • Clock: 17
  • 3x3: 25
  • 4x4: 40
  • Square-1: 40
  • 5x5: 60
  • Megaminx: 70
  • 6x6: 80
  • 7x7: 100

Category 3 scripts are safe for use in all projects. Category 4 scripts generate higher-quality scrambles, but may be of impractical length. All scripts are named by category and puzzle followed by byte count and author.

NOTE: Scramblers for puzzles that have no parallel moves are listed under category 2 if they perform move cancellation.

for(a=11,x=Math.random;a--;document.write('RUF'[0|x()*3]+" 2'"[0|x()*3]+' '));
for(a=25,x=Math.random;a--;document.write('RLUDFB'[0|x()*6]+" '2"[0|x()*3]+' '));
r=Math.random;for(j=25;j--;document.write("URFBLD"[m]+" 2'"[0|r()*3]+" "))m=0|r()*6
for(a=11,x=Math.random;a--;document.write('RLUB'[0|x()*4]+" '"[0|x()*2]+' '));
for(a=y=r='',x=Math.random;a++<11;document.write(r+" 2'"[0|x(y=r)*3]+' '))for(;r==y;r='RUF'[0|x()*3]);
r=Math.random;for(b=j=25;j;b-m?document.write("URFBLD"[j--,b=m]+" 2'"[0|r()*3]+" "):0)m=0|r()*6
for(a=y=r='',x=Math.random;a++<25;document.write(r+" '2"[0|x(y=r)*3]+' '))for(;r==y;r='RLUDFB'[0|x()*6]);
for(a=p=l=[],r=Math.random;l++<25;)a.push((p='RLUDFB'.replace(p,'')[0|r()*5])+" '2"[0|r()*3]);console.log(a.join(' '))
for(a=y=r='',x=Math.random;a++<11;document.write(r+" '"[0|x(y=r)*2]+' '))for(;r==y;r='RLUB'[0|x()*4]);
for(c=b=j=25,r=Math.random;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
//es6 only
for(j=7;j--;)document.write(["UF'UF'RFU2R2F'R'","RU'R'UF2UF2U'F","R'F2RU'FU'F'U2","R'F'R2U2FUF'UR'","U2F2UF2UR'F'RF","F2R2U2RUF","F2R'F'RF2R'UFU2"][j].repeat(3*(j+1)*Math.random()))
//es6 only
y=["R","R2","R'","RU","RU2"],z=[v="L2F2D'L'DF'LD'LD",v+v];document.write([["F","F2","F'","U'F'","UF2","RF"],x=["R'","R2","R","U'R'","U2R'"],["R'U'R","R'UR","B'U'B","R'U2R"],["U","U2","U'"],[w="R'ULU'RUL'U'",w+w],["U"+w+"U2"]].map(e=j=>{return j[0|Math.random()*(1+j.length)]||""}).join("")+[0,1,2,3,4].map((i)=>{return x[i]+e(z)+y[i]}).join("")+e(z))
//es6 only
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)
//es6 only
for(j=8;j--;)document.write(["UF'R'U2F2LBR'UF'","F'UL'U'L'F'LF2","U'RFU'BR'U2LD'","RU'LDF2L2D'R'F","F2UR'F'L'D'RF'U","U2F'R2UF'DL","RU2F'UL'DBR2B2","FU'RFR'F2R'F'D'F"][j].repeat(3*(j+1)*(r=Math.random)()));for(j=12;j--;)document.write(["F2L2F2UL'RBU2L2U2B'LR'U'","R2FRBF2UL2U'F'LB'F2URU","L'D'B'LU'BDFL'F2L2F","B'RF'D2FRB2F2L2BDB2R2F2","B2LR2D'R'D2L'D'R'BU'B2L'B'","L2B'L2BL2B2L'B'U'F'L'D'L2DLF","B'L2BFU2B2R'D2F'D2R'D2BR2UL2","U2B2U2F2DL'FU'B'ULBF2R'F'R","D2L2B2D2R2U'B2F2LB'L2R2FLB2R2DB2","L2D2BL2B'LDB'L2U'F2L'FU'F'UL'F'","F2R2D'U2L2R2B'F2R2UL2FLF'LFUF'","DB2D2U'F2LBD2LR'DB2U2RUL2U'B"][j].repeat(2*(j+1)*r()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.