Dihedral group D4 composition with custom labels The dihedral group \$D_4\$ is the symmetry group of the square, that is the moves that transform a square to itself via rotations and reflections. It consists of 8 elements: rotations by 0, 90, 180, and 270 degrees, and reflections across the horizontal, vertical, and two diagonal axes.



The images are all from this lovely page by Larry Riddle.

This challenge is about composing these moves: given two moves, output the move that's equivalent to doing them one after another. For instance, doing move 7 followed by move 4 is the same as doing move 5.



Note that switching the order to move 4 then move 7 produces move 6 instead.

The results are tabulated below; this is the Cayley table of the group \$D_4\$. So for example, inputs \$7, 4\$ should produce output \$5\$.

\begin{array}{*{20}{c}}
   {} &amp; {\begin{array}{*{20}{c}}
   1 &amp; 2 &amp; 3 &amp; 4 &amp; 5 &amp; 6 &amp; 7 &amp; 8 \\ 
\end{array} }  \\ 
   {\begin{array}{*{20}{c}}
   1  \\ 
   2  \\ 
   3  \\ 
   4  \\ 
   5  \\ 
   6  \\ 
   7   \\
   8   \\
\end{array} } &amp; {\boxed{\begin{array}{*{20}{c}}
   1 &amp; 2 &amp; 3 &amp; 4 &amp; 5 &amp; 6 &amp; 7 &amp; 8 \\ 
   2 &amp; 3 &amp; 4 &amp; 1 &amp; 8 &amp; 7 &amp; 5 &amp; 6\\ 
   3 &amp; 4 &amp; 1 &amp; 2 &amp; 6 &amp; 5 &amp; 8 &amp; 7\\ 
   4 &amp; 1 &amp; 2 &amp; 3 &amp; 7 &amp; 8 &amp; 6 &amp; 5\\ 
   5 &amp; 7 &amp; 6 &amp; 8 &amp; 1 &amp; 3 &amp; 2 &amp; 4\\ 
   6 &amp; 8 &amp; 5 &amp; 7 &amp; 3 &amp; 1 &amp; 4 &amp; 2\\ 
   7 &amp; 6 &amp; 8 &amp; 5 &amp; 4 &amp; 2 &amp; 1 &amp; 3\\ 
   8 &amp; 5 &amp; 7 &amp; 6 &amp; 2 &amp; 4 &amp; 3 &amp; 1\\ 
\end{array} }}  \\ 
\end{array}

Challenge

Your goal is to implement this operation in as few bytes as possible, but in addition to the code, you also choose the labels that represent the moves 1 through 8. The labels must be 8 distinct numbers from 0 to 255, or the 8 one-byte characters their code points represent.

Your code will be given two of the labels from the 8 you've chosen, and must output the label that corresponds to their composition in the dihedral group \$D_4\$.

Example

Say you've chosen the characters C, O, M, P, U, T, E, R for moves 1 through 8 respectively. Then, your code should implement this table.

\begin{array}{*{20}{c}}
   {} &amp; {\begin{array}{*{20}{c}}
   \, C  \, &amp; \, O \, &amp; M \, &amp; P \, &amp; U \, &amp; \, T \, &amp;  \, E \,  &amp;  R \, \\ 
\end{array} }  \\ 
   {\begin{array}{*{20}{c}}
   C  \\ 
   O  \\ 
   M  \\ 
   P  \\ 
   U  \\ 
   T  \\ 
   E   \\
   R   \\
\end{array} } &amp; {\boxed{\begin{array}{*{20}{c}}
   C &amp; O &amp; M &amp; P &amp; U &amp; T &amp; E &amp; R \\ 
   O &amp; M &amp; P &amp; C &amp; R &amp; E &amp; U &amp; T\\ 
   M &amp; P &amp; C &amp; O &amp; T &amp; U &amp; R &amp; E\\ 
   P &amp; C &amp; O &amp; M &amp; E &amp; R &amp; T &amp; U\\ 
   U &amp; E &amp; T &amp; R &amp; C &amp; M &amp; O &amp; P\\ 
   T &amp; R &amp; U &amp; E &amp; M &amp; C &amp; P &amp; O\\ 
   E &amp; T &amp; R &amp; U &amp; P &amp; O &amp; C &amp; M\\ 
   R &amp; U &amp; E &amp; T &amp; O &amp; P &amp; M &amp; C\\ 
\end{array} }}  \\ 
\end{array}

Given inputs E and P, you should output U. Your inputs will always be two of the letters C, O, M, P, U, T, E, R, and your output should always be one of these letters.

Text table for copying

1 2 3 4 5 6 7 8
2 3 4 1 8 7 5 6
3 4 1 2 6 5 8 7
4 1 2 3 7 8 6 5
5 7 6 8 1 3 2 4
6 8 5 7 3 1 4 2
7 6 8 5 4 2 1 3
8 5 7 6 2 4 3 1
Ruby, 18 bytes



-&gt;a,b{a+b*~0**a&amp;7}


Ungolfed

-&gt;a,b{ (a+b*(-1)**a) % 8}  
# for operator precedence reasons, 
#-1 is represented as ~0 in the golfed version 


Try it online!

Uses the following coding numbers 0 to 7

In order native to the code:

Native     Effect                    Codes per
Code                                 Question
0          rotate 0 anticlockwise    1C
1 /        flip in y=x               7E
2 /|       rotate 90 anticlockwise   2O
3 /|/      flip in x axis            5U
4 /|/|     rotate 180 anticlockwise  3M
5 /|/|/    flip in y=-x              8R
6 /|/|/|   rotate 270 anticlockwise  4P
7 /|/|/|/  flip in y axis            6T


In order per the question

Native     Effect                    Codes per
Code                                 Question
0          rotate 0 anticlockwise    1C
2 /|       rotate 90 anticlockwise   2O
4 /|/|     rotate 180 anticlockwise  3M
6 /|/|/|   rotate 270 anticlockwise  4P
3 /|/      flip in x axis            5U
7 /|/|/|/  flip in y axis            6T
1 /        flip in y=x               7E
5 /|/|/    flip in y=-x              8R


Explanation

/ represents a flip in the line y=x and | represents a flip in the y axis.

It is possible to generate any of the symmetries of the group D4 by alternately flipping in these two lines For example / followed by | gives /| which is a rotation of 90 degrees anticlockwise.

The total number of consecutive flips gives a very convenient representation for arithmetic manipulation.

If the first move is a rotation, we can simply add the number of flips:

Rotate 90 degrees   +  Rotate 180 degrees = Rotate 270 degrees
/|                     /|/|                 /|/|/|

Rotate 90 degress   +  Flip in y=x        = Flip in x axis   
/|                    /                     /|/


If the first move is a reflection, we find we have some identical reflections / and | symbols next to each other. As reflection is self inverse we can cancel out these flips one by one. So we need to subtract one move from the other

Flip in x axis     +  Flip in y=x        = Rotate 90 degrees
/|/                   /                    /|/ / (cancels to) /|

Flip in x axis     +  Rotate 90 degrees  = Flip in y=x
/|/                   /|                   /|/ /| (cancels to ) /