20
2
Whenever you make a move on a Rubik's Cube, there is a reverse move which undoes the first move. Because of this, every algorithm (set of moves) has a reverse algorithm which undoes the first algorithm.
The goal of this challenge is to find the reverse of a given algorithm.
Specification:
The input consists of an array of individual moves. Each move is a string of length 1 or 2. Of course, you can use whatever input format makes the most sense in your language. Each move consists of the structure X
or X'
or X2
, where X
is an uppercase or lowercase letter.
To reverse X
, simply replace it with X'
. Likewise, X'
becomes X
. X2
on the other hand does not get changed.
To create the output, reverse each move, and then reverse the array.
Examples (strings separated by spaces):
R
=> R'
D U'
=> U D'
S T A C K
=> K' C' A' T' S'
A2 B2
=> B2 A2
Scoring:
This is code-golf, so the fewest amount of bytes win. Standard loopholes are not allowed.
Is
R2
->R2'
orB
->B3
allowed? – CalculatorFeline – 2017-07-06T18:01:44.800R2
has to becomeR2
. AlthoughB3
makes sense for actual algorithms, it is not within the scope of the challenge. – Julian Lachniet – 2017-07-06T18:04:37.3972Having to handle
X3
orX1
would have been a nice addition to the challenge. – Shaggy – 2017-07-06T18:32:56.8971"Because of this, every algorithm (set of moves) has a reverse algorithm which undoes the first algorithm" is this true for every algorithms?? Cause I think the hashing algorithms are one way. Means it doesn't have any reverse algorithms, right? please let me know – Avishek Saha – 2017-07-07T04:21:12.613
4@AvishekSaha : For Rubik's Cube problems, "algorithm" is restricted to the meaning "a sequence of moves you can do on the Cube". In this sense, there is no such thing as a one-way hashing algorithm on the Cube. – Ross Presser – 2017-07-07T04:35:42.540
oh! I was talking about algorithms in general, not the rubik's cube algorithms though. – Avishek Saha – 2017-07-07T04:37:55.010
@AvishekSaha It is not true for general algorithms. If an algorithm takes both A and B to C, then a reverse algorithm ran on C would on one hand have to return A, and on the other it would have to return B. The theory of reversibke computing is interesting, but quite different from your usual computer science.
– Wojowu – 2017-07-07T17:41:45.7035Should have had
D2R2
as a test case... – Neil – 2017-07-29T15:49:42.037Can we assume that the input only contains valid Rubik's Cube moves? For example, if the input
RL
produced the outputL'R'
, but the inputRLK
also produced the outputL'R'
, would the answer be valid? – MD XF – 2018-01-24T05:40:09.127