1
\$\begingroup\$

Okay, I recently wrote a Javascript Rubik's Cube scrambler that outputs a 25-move scramble. It's 135 bytes. The Gist is here, but for reference, I have the formatted code:

function(){
  for(a=s=y=r=[],m='RLUDFB',x=Math.random;++a<26;y=r,s.push(r+["'",2,''][0|x()*2]))
  for(;r==y;r=m[0|x()*5]);
  return s.join(' ')
}

Any tips?

EDIT: Code is updated, with @Optimizer's edits.

\$\endgroup\$
3
  • \$\begingroup\$ I am assuming that you need to run this in browser, so ES5 and below only ? \$\endgroup\$
    – Optimizer
    May 29, 2015 at 22:34
  • \$\begingroup\$ Some of the answers are likely to be complete rewrites rather than tips for lowering the byte count of your algorithm/implementation. \$\endgroup\$
    – Sparr
    May 29, 2015 at 22:35
  • \$\begingroup\$ @Optimizer Yeah, you have to run in a browser console. \$\endgroup\$ May 29, 2015 at 22:40

1 Answer 1

0
\$\begingroup\$

140 135 127 bytes

Still a big one, but here's for starters:

function(){for(a=s=y=r=[],x=Math.random;a<25;s[a++]=r+["'",2,''][0|x(y=r)*3])for(;r==y;r='RLUDFB'[0|x()*6]);return s.join(' ')}

Some of the few tips used:

  • Loose the var. There is no point in using var when your aim is to golf!
  • When a = [], a++ gives 0. We use this fact to remove a 0 and ,
  • Combine the a<25 and a++
  • You don't need to split a string. You can access its characters anyways via [i]
  • Get rid of {} in for loops

More to come.

\$\endgroup\$
2
  • \$\begingroup\$ Just wondering, why are the parentheses after the function not there? \$\endgroup\$ May 29, 2015 at 23:11
  • \$\begingroup\$ @molarmanful Whoops \$\endgroup\$
    – Optimizer
    May 29, 2015 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.