# Cayley Table of the Dihedral Group $D_3$

The Dihedral group $D_3$ represents the symmetries of an equilateral triangle, using the identity (represented by id), rotations (represented by r1 and r2), and reflections (represented by s0, s1, and s2).

Your task is to compute the composition $yx$ of the elements $x, y \in D_3$. They are given by the Cayley table below:

  x  id  r1  r2  s0  s1  s2
y  +-----------------------
id | id  r1  r2  s0  s1  s2
r1 | r1  r2  id  s1  s2  s0
r2 | r2  id  r1  s2  s0  s1
s0 | s0  s2  s1  id  r2  r1
s1 | s1  s0  s2  r1  id  r2
s2 | s2  s1  s0  r2  r1  id


### Input

Any reasonable input of x and y. Order does not matter.

### Output

y composed with x, or looking up values in the table based on x and y.

### Test Cases

These are given in the form x y -> yx.

id id -> id
s1 s2 -> r1
r1 r1 -> r2
r2 r1 -> id
s0 id -> s0
id s0 -> s0


### Notes on I/O

You may use any reasonable replacement of id, r1, r2, s0, s1, s2, for example 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, or even [0,0], [0,1], [0,2], [1,0], [1,1], [1,2] (here the first number represents rotation/reflection and the second is the index).

# Python 2, 27 bytes

lambda o,O:[o[_]for _ in O]


Try it online!

• can u explain this code? – tarit goswami Sep 16 '18 at 9:07
• @taritgoswami Both o and O are three-element lists containing a permutation of the integers 0, 1, 2. In the list comprehension, the former is indexed by the latter, implementing permutation composition. – Jonathan Frech Sep 16 '18 at 10:25

# Jelly, 1 byte

ị


A dyadic link taking y on the left and x on the right.

Uses the representations of the fist three natural numbers transformed as their actions describe:

   name:  id          r1          r2          s0          s1          s2
value:  [1,2,3]     [2,3,1]     [3,1,2]     [2,1,3]     [1,3,2]     [3,2,1]
(action:  identity    rot-Left    rot-Right   swap-Left   swap-Right  swap-Outer)


A port of Jonathan Frech's Python answer

ị is Jelly's "index into" atom, and it vectorises; note that Jelly is 1-indexed.

To take x on the left and y on the right, these values may be used instead:

id       r1       r2       s0       s1       s2
[1,2,3]  [3,1,2]  [2,3,1]  [1,3,2]  [3,2,1]  [2,1,3]


...see here.

# JavaScript (ES6), 39 bytes

Uses the following mapping:

 id | r1 | r2 | s0 | s1 | s2
----+----+----+----+----+----
2  | 0  | 4  | 1  | 3  | 5


Takes input as (x)(y).

x=>y=>'450123234523012323'[(x*51^y)%18]


Try it online!

# JavaScript (ES6), 20 bytes

x=>y=>y.map(n=>x[n])


Try it online!

# Python 2, 2726 23 bytes

lambda x,y:(y+x*5**y)%6


Try it online! Edit: Saved 3 bytes thanks to @NieDzejkob. Uses the following mapping:

 id | r1 | r2 | s0 | s1 | s2
----+----+----+----+----+----
0  | 2  | 4  | 1  | 3  | 5

• @JonathanFrech ... I don't have x*-1... I have x*(-1**y) – Neil Sep 12 '18 at 11:16
• @JonathanFrech Ah, I'd typoed my test code, I thought I was getting (-1)**y. Oh well, that's still 1 byte shorter... – Neil Sep 12 '18 at 13:18
• Hm ... And I thought I golfed a byte ... – Jonathan Frech Sep 12 '18 at 16:36
• Like on the D4 challenge, you can replace (-1) with 5 for -3 bytes. – NieDzejkob May 4 at 14:11

# APL (Dyalog Classic), 3 bytes

+.×


Try it online!

+.× is matrix multiplication

we represent the group as

id     r1     r2     s0     s1     s2
1 0 0  0 0 1  0 1 0  0 0 1  0 1 0  1 0 0
0 1 0  1 0 0  0 0 1  0 1 0  1 0 0  0 0 1
0 0 1  0 1 0  1 0 0  1 0 0  0 0 1  0 1 0


# K (ngn/k), 1 byte

@


Try it online!

x@y is list indexing, which is the same as composition of permutations; we represent the group as

id:0 1 2; r1:1 2 0; r2:2 0 1; s0:2 1 0; s1:1 0 2; s2:0 2 1


# Japt, 2 bytes

gV


Try it online

# JavaScript (Node.js), 24 19 bytes

(x,y)=>(y+x*5**y)%6


Try it online! Edit: Saved 2 bytes by switching to ** and 3 bytes thanks to @NieDzejkob. Uses the following mapping:

 id | r1 | r2 | s0 | s1 | s2
----+----+----+----+----+----
0  | 2  | 4  | 1  | 3  | 5


The old 24 byte version also works in old versions of JavaScript:

(x,y)=>(y%2?y+6-x:y+x)%6


# Racket, 42 bytes

(lambda(x y)(modulo(+ y(* x(expt 5 y)))6))


Try it online!

A boring port of Neil's Python answer. Uses the same I/O format, so:

 id | r1 | r2 | s0 | s1 | s2
----+----+----+----+----+----
0  | 2  | 4  | 1  | 3  | 5


# 05AB1E, 1 byte

è


Port of @JonathanAllan's Jelly answer, but with 0-based indices, so the representations are:

id       r1       r2       s0       s1       s2       # Original values
[0,1,2]  [1,2,0]  [2,0,1]  [1,0,2]  [0,2,1]  [2,1,0]  # Values instead


Explanation:

è  # Index the second (implicit) input-list vectorized into the first (implicit) input-list
# And output the result implicitly


# Wolfram Language (Mathematica), 16 bytes

Mod[#+#2*5^#,6]&


Try it online!

Another boring port of Neil's answer, this time in Mathematica. It is an anonymous function that takes arguments in the order [y, x].

Here’s the input representation:

 id | r1 | r2 | s0 | s1 | s2
----+----+----+----+----+----
0  | 2  | 4  | 1  | 3  | 5