# What would be the most efficient way to reduce the complexity of a set of rotations to a cube?

Despite the long title, my question is fairly simple. Let's say you have a cube in 3D-space and you want to rotate it along the three axes based on the set of instructions provided by a user or elsewhere in the program. How would you simplify, if you will, that set of rotations?

For simplicity's sake, we'll use the notation involved in rotating a Rubik's cube. (And also because that's what I'm programming.. But that's unimportant.) So, X would be a clockwise rotation along the X axis, X' would be a counterclockwise rotation along the X axis, and X2 would be two clockwise rotations rotations along the X axis.

Now, let's say I have a set of instructions `X Y X'`. That set of instructions did the same thing that the single instruction of `Z` would have done. Or, in a simpler case, if I had `X X'` or `X2 X2`, those rotations cancel out.

My question is, what is the best way - from a programming perspective - to simplify these sets of instructions?