Identity sequences on the Rubik's Cube
Haskell, 263 261 247 243 characters
c[x]=[x]
c(x:"2")=[x,x]
c(x:_)=[x,x,x]
s!a@[x,y,z]=case s of
'R'|x>0->[x,-z,y]
'B'|y>0->[z,y,-x]
'U'|z>0->[-y,x,z]
'L'|x<0->[x,z,-y]
'F'|y<0->[-z,y,x]
'D'|z<0->[y,-x,z]
_->a
r=[-2..2]
i=mapM id[r,r,r]
f w=i==foldr(map.(!))i(c=<<words w)
Rather straightworward algorithm; each cubelet is made out of 1,2,4 or 8 chunks encoding its position and orientation; 4 chunks per edge cubelet, 8 per corner cubelet, 7 cubelets are stationary.
c
chomps each word of the input into a sequence of CW turns, and !
sends each chunk according to a turn. i
is the identity position. f
is the main function.
I'm not too happy with the c
homp function, but I can't seem to find a way to shorten it either (@Nimi did, however)
Cubically, 6 4 bytes
¶=8%
I win :P
¶=8%
¶ read a string, evaluate as Cubically code
=8 set notepad to (notepad == 8th face)
% print notepad
The notepad is initialized to zero. The 8th "face" contains 1 if the cube is unsolved and 0 otherwise.
Try it online!
J - 232, 220, 381, 315 296 bytes
This solution encodes all operations as face permutations and works based on the fact that all face twists are actually the same, under a rotation of the entire cube.
Edit: some more golfing
f=:+/~6&*
r=:4 :'y f&.>(]{^:x~)&.C.;/i.2 4'"0
t=:((r~0),_4<\44#.inv 1478253772705907911x)&C.&.
Y=:(C.(,0 2 r 4 5),;/4 f&i.8)&{^:t
X=:((,1 1 0 2 r 2 4 3 1)C.C.;/0 4 2 5 f i.8)&{^:t
61".@A."1'=: ',"#~6 3$'D0XR1YF1XU2YB3XL3Y'
T=:[:(_2".@}.'(i.48)-:'&(,,[))[:(,'^:',])/&.>@|.&.;:[:''''&=@{.`]},:&'3'
Other than the previous tries, this does take corner rotation into account.
f
is just a helper function. r
does the rotation of one face. a face is encoded as follows:
- all corners in steps of 6
- all edges in steps of six
this order facilitates the encoding of rotations and twists.
t
is an adverb that twists the face under a certain cube rotation, selecting the face.
X
and Y
are adverbs which take as left argument the number of in that direction of the entire cube.
The next line defines all rotations: 3 characters per rotation: the name, the number of rotations and the direction.
The last line defines the test verb T
, converting 3 and '
to Power notation, flipping the order of operation appending the test vector andfinally excuting the entire thing.
More details upon request.
tests =: (] ;. _2) 0 : 0
U2 R R' U2
U D2 U' D2
U2 R2 R'
R' U' R' F' U F U' R' F R F' U' R U2 R
L'
B B2 B' B2
D D2 D'
R F' D2 U B' F2 B' U2 D2 F2 B2 U F R'
D2 U' R2 U F2 D2 U' R2 U' B' L2 R' B' D2 U B2 L' D' R2
R U R' U' R' F R2 U' R' U' R U R' F' R2 U R2 U' R2 U' D R2 U' R2 U R2 D'
R U R' U' R' F R2 U' R' U' R U R' F' R2 U' R2 U R2 U' D R2 U' R2 U R2 D'
B2 F2 U' F2 U R2 F2 U2 B D' R' D' R2 D' F2 U' F U R2 U R B D B D2 L2 D' F2 U D' R' D B R2 D2 F2 R' F2 D2
R U2 R' U R' U2 R U2 R U R' U' R' U R U2
U F B' R' U F' R U' F' B L U' F L'
R2 U' R' U' R U R U R U' R
R' F R' B2 R F' R' B2 R2
)
res =: 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0
res ([,],:=) T"1 tests NB. passes all tests.
1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0
1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
NB. some handy display methods:
dispOrig=: (". ;._2) 0 :0
_ _ _ 5 29 11 _ _ _ _ _ _
_ _ _ 47 _1 35 _ _ _ _ _ _
_ _ _ 23 41 17 _ _ _ _ _ _
3 27 9 0 24 6 1 25 7 2 26 8
45 _3 33 42 _6 30 43 _5 31 44 _4 32
21 39 15 18 36 12 19 37 13 20 38 14
_ _ _ 4 28 10 _ _ _ _ _ _
_ _ _ 46 _2 34 _ _ _ _ _ _
_ _ _ 22 40 16 _ _ _ _ _ _
)
ind =: dispOrig i.&, i. 48 NB. indices of i.48 in the original display
disp =: (9 12$(,dispOrig) ind}~ ])
changed =: 1 : '(u ~:&disp ]) i.48' NB. use to debug permutation verbs: L ch
vch =: 1 :'disp ((]+_*=) u)'
NB. viewmat integration RGB
cm =: 255 * 1 0 0 , 1 1 1, 0 1 0, 1 1 0, 1 0.5 0, 0 0 1,: 0 0 0 NB. colormap
NB. use as: "cube i. 48" for seeing a nice folded out cube.
cube =: cm viewmat (>&7 + >&15 + >&23 + >&31 + >& 39 + >&47)@|@disp@]