8
\$\begingroup\$

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).

\$\endgroup\$

11 Answers 11

8
\$\begingroup\$

Python 2, 27 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ can u explain this code? \$\endgroup\$ – tarit goswami Sep 16 '18 at 9:07
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Jonathan Frech Sep 16 '18 at 10:25
3
\$\begingroup\$

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.

Try it online! Or see a table using the question-names.


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.

\$\endgroup\$
3
\$\begingroup\$

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

Using Jonathan Frech's I/O format:

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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

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 
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Japt, 2 bytes

Uses Jonathan's I/O format.

gV

Try it online

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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  
\$\endgroup\$
0
\$\begingroup\$

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

Try it online or verify all possible combinations or verify all mapped back to ids.

Explanation:

è  # Index the second (implicit) input-list vectorized into the first (implicit) input-list
   # And output the result implicitly
\$\endgroup\$
0
\$\begingroup\$

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 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.