3x3x3 simulator in 745 bytes of javascript

The following layout allows shorter initialization as well as shorter indices (uses all of 0 to 9 instead of just 3 to 5):

YYYZZZAAACCC
YYYZZZAAACCC
YYYZZZAAACCC
BBB
BBB
BBB
XXX
XXX
XXX

Edit:

And you could replace
[noparse]if(q==37){for(i=0;i++<99;)d(0|Math.random()*3+38)}[/noparse]
with
if(i=q>99)while(i--)d(0|Math.random()*3+38)
 
626

I changed the ending like proposed above, changed the r function and how it's called, and assigned w when it's first used.

a=document.getElementById('a');h='XXXYYYZZZAAABBBCCC\n';a[w="innerHTML"]=h+h+h;function r(s){g=a[w].split("");for(j=0;++j<s.length;)if(j%4){f=g[x=s[j]];g[x]=g[y=s[j-1]];g[y]=f};a[w]=g.join("")}window.onkeydown=d;function d(e){q=e.keyCode||e;if(q==38)r([0,2,40,38,1,21,39,19,3,12,9,6,4,13,10,7,5,14,11,8]);if(q==39)d(38),r([23,32,29,26,22,31,28,25,24,33,30,27,41,50,47,44,42,51,48,45,43,52,49,46,15,53,55,17,16,34,54,36]);if(q==40)r([0,3,15,49,1,4,16,48,2,5,17,47,19,22,34,30,20,23,35,29,21,24,36,28,38,41,53,11,39,42,54,10,40,43,55,9,6,44,46,8,7,25,45,27,12,14,52,50,13,33,51,31]);if(q>99)for(i=q;i--;)d(0|Math.random()*3+38)}
 
Last edited:
This is pretty crazy stuff, although we are certainly pushing the limit of what we can call a sim :p

I don't know if this counts as cheating, but I saved 23(?) characters by relaxing our definition of a scramble (and replacing a "40" with a "q"). Hold left (or any key other than up/right/down, really) to do random moves. http://jsfiddle.net/c8uNB/4/

I wonder if we could gain anything by changing the definition of a[w]/h to something like "h='XYZABC\n';h+=h+h;a[w="innerHTML"]=h+h+h;". We'd have to change the permutations of course.


EDIT: You're gonna hate me for this one. http://jsfiddle.net/c8uNB/5/
EDIT2: Saved another character by switching from ascii(whatever+60) to ascii(whatever+35). http://jsfiddle.net/c8uNB/6/
 
Last edited:
This is very impressive, reminds me of 96kb .kkrieger

EDIT2: Saved another character by switching from ascii(whatever+60) to ascii(whatever+35). http://jsfiddle.net/c8uNB/6/

I saved 3 characters by a cheap trick (just like the one used with innerHTML).
http://jsfiddle.net/c8uNB/10/


(I also have a fail attempt here, I cannot understand why it fails to work... can any js gurus enlighten me? I'm probably forgetting some basic js concept...)
 
Back
Top