Improving a Rubik's Cube scrambler's byte count

1

0

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.

Mama Fun Roll

Posted 2015-05-29T22:31:27.467

Reputation: 7 234

I am assuming that you need to run this in browser, so ES5 and below only ? – Optimizer – 2015-05-29T22:34:03.540

Some of the answers are likely to be complete rewrites rather than tips for lowering the byte count of your algorithm/implementation. – Sparr – 2015-05-29T22:35:27.793

@Optimizer Yeah, you have to run in a browser console. – Mama Fun Roll – 2015-05-29T22:40:33.750

Answers

0

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.

Optimizer

Posted 2015-05-29T22:31:27.467

Reputation: 25 836

Just wondering, why are the parentheses after the function not there? – Mama Fun Roll – 2015-05-29T23:11:33.670

@molarmanful Whoops – Optimizer – 2015-05-29T23:17:05.330