32
11
A move sequence is a sequence of moves (turns) on a Rubik's Cube (for the notation look down below). Beside the empty move sequence, there are many other other move sequences, that have no effect on the cube at all. We call these move sequences identity sequences.
Some of these identity sequences are obvious to determine, like U2 R R' U2
or U D2 U' D2
. In the first one, two random moves are done U2 R
and afterwards immediately undone R' U2
. The second one is similar. First two random moves U D2
and afterwards they are undone, but in reversed order U' D2
. This only works, because the move U
effects only the pieces of the upper layer and the move D2
only effects pieces of the lower layer. You can see a visualization of these two move sequences.
Other identity sequences may be not obvious at all. For instance the sequence R' U' R' F' U F U' R' F R F' U' R U2 R
. It pretty long, but also has no effect at the cube at all.
Move Notation
A move describes the turn of one layer of one of the six faces of the cube. A move consist of one letter representing the face followed by an optional suffix representing the turn angle.
The letters and their corresponding faces are U (Up - the side facing upwards), D (Down - the side facing downwards), R (Right - the side facing to the right), L (Left - the side facing to the left), F (Front - the side facing you) and B (Back - the side facing away from you).
If there is no suffix, the face is turned 90-degree clockwise, the suffix '
means, the face is turned 90-degree counterclockwise, and the suffix 2
means, the face is turned 180-degree clockwise.
It you have any problems with the notation, just use http://alg.cubing.net, where you can visualize such move sequences.
The Challenge
Your task is to write a program, that determines if a move sequences is an identity or not.
You may write a full program or a function. It should receive a string containing a move sequence (moves are separate by spaces) as input (via STDIN, command-line argument, prompt or function argument), and output (via return value or STDOUT) a Boolean value or a corresponding integer (True - 1 - identity sequence/ False - 0 - not identity sequence).
If you suffix '
creates problems in your programming language, you may use a different symbol, but not at digit. R F2 U3
is not allowed.
This is codegolf, therefore the shortest code (in bytes) wins.
Test Cases
"" -> True
"U2 R R' U2" -> True
"U D2 U' D2" -> True
"U2 R U2 R'" -> False
"R' U' R' F' U F U' R' F R F' U' R U2 R" -> True
"L'" -> False
"B B2 B' B2" -> True
"D D2 D'" -> False
"R F' D2 U B' F2 B' U2 D2 F2 B2 U F R'" -> True
"D2 U' R2 U F2 D2 U' R2 U' B' L2 R' B' D2 U B2 L' D' R2" -> False
"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'" -> True
"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'" -> False
"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" -> True
"R U2 R' U R' U2 R U2 R U R' U' R' U R U2" -> False
"U F B' R' U F' R U' F' B L U' F L'" -> False
"R2 U' R' U' R U R U R U' R" -> False
"R' F R' B2 R F' R' B2 R2" -> False
What's wrong with
R F2 U3
? – John Dvorak – 2015-01-21T12:37:41.2002I just want to make sure, that everybody has the same preconditions. If I would allow
U3
, than you could simply cast the suffix into a digit. – Jakube – 2015-01-21T12:47:43.547Why does that matter? Input parsing is not that big a part of the challenge. – John Dvorak – 2015-01-21T12:52:31.530
Sure, it doesn't matter a lot, but I want to people to use the original notation, since this is the standard notation (Singmaster notation). Just use the
'
, if that's works with your programming language. – Jakube – 2015-01-21T13:23:54.3103I'm more used to the notation that uses T-Top, B-Bottom, and P-Posterior (back). People probably just liked seeing the sequence
R2 D2
. – mbomb007 – 2015-01-21T15:30:59.0802@mbomb007 I can understand T for top, but I've never seen P for posterior and I wouldn't understand its meaning were it not for your comment... – John Dvorak – 2015-01-21T16:17:30.000
The notation is used in an older Rubik's Cube solution book. It's probably 20-30 years old, but that's the only solution book I've ever looked at. I didn't even know about the current notation until I did a project last year on Rubik's Cube for Abstract Algebra. – mbomb007 – 2015-01-21T16:22:47.357
2@mbomb007 I have seen that notation too, but it is neither as common or as old as the original Singmaster notation, and I don't know why people want to mess with the original. Although David Singmaster (as far as I know) did not mention it, I have observed that all faces are perfectly consistent and with no clashes if considered as directions rather than positions.
That is F(orward), B(ackward), L(eft), R(ight), U(p), D(own)
– Level River St – 2015-01-21T19:04:37.770I know the intentions are not such, but currently all test cases will pass even if you do not care about the orientation of the cubies. For example, if a cube is scrambled in such a way that only OLL step is remaining, and I call that cube as identity cube and write by program keeping that in minds, the test cases will still pass. You need to add test cases which dis orient the layers, even though keeping the position correct. For ex. twisted corners, reversed sides etc. – Optimizer – 2015-01-22T09:28:35.397
Good point. I will add some tests, as soon I arrive at home. – Jakube – 2015-01-22T10:54:08.300
Okay, fine! I'll fix mine! – KSFT – 2015-01-26T16:44:32.607
Can we swap truthy/falsy output values? For example, if I specify in my answer, can
R2 R2
output0
andR2
output1
? – MD XF – 2017-12-03T00:40:46.837@MDXF Yes, I did't explicitly forbid that. But I see that you already found a different way, or can solve the problem in less than 4 bytes without that restriction? – Jakube – 2017-12-03T10:03:58.823
@Jakube Yes, I found a different way. Swapping truthy/falsy values wouldn't get it below 4 bytes. – MD XF – 2017-12-03T22:36:07.097