6
\$\begingroup\$

Two dimensional chiral objects are not chiral in three dimensional space. This is because you can flip them over which is equivalent to taking the mirror image unlike in two dimensions where only rotations in the plane of the object are allowed which cannot reproduce the effect of a mirror operation. In this challenge, we will be deciding whether three dimensional texts are chiral.

Some definitions

  • Three dimensional text: A rectangular three dimensional array of characters. In this challenge, programs will only need to handle nonwhitespace printable ASCII (! through ~).
  • Mirror operation: The operation of reversing the order of characters along a single axis of the three dimensional text.

Rotations can be thought of intuitively, but here is an explicit definition for those who will eventually ask.

  • 90-degree rotation: The operation of transposing each plane perpendicular to a specific axis (say \$i\$), then applying a mirror operation to an axis other than \$i\$.
  • Rotation: A succession of 90-degree rotations.
  • Chiral: A three dimensional text is chiral if after applying any single mirror operation, there does not exist a rotation of the new text that reproduces the original text. A text is not chiral if after applying a single mirror operation the new text can be rotated back to the original text.

A slightly more concise definition of chirality is lack of existence of an improper rotation under which an object is invariant.

Task

In whatever format you prefer, take a three dimensional text as input.

Output one of two consistent values, one indicating that the text is chiral and one indicating that it is not.

Shortest code wins.

Examples

In each section, the vertical pipes (|) separate the test cases. Within each test case, layers of the three dimensional texts are separated by blank lines.

Not chiral

Z--Z    |    i~    |    Qx    |    a    |    abaLIa
----    |    >>    |    xx    |    b    |    N#)*@"
Z--Z    |          |          |         |    }-3}#k
        |    >>    |    QQ    |    d    |    ../..'
Z--Z    |    ~i    |    QQ    |    e    |    
----    |          |          |         |    
Z--Z    |          |          |    f    |    
        |          |          |    g    |    

Chiral

Z--Z    |    *k    |    Qxxx    |    a*
----    |    <0    |    xxxx    |    **
Z---    |          |            |    
        |    l7    |    QQQQ    |    **
Z---    |    GB    |    QQQQ    |    a*
----    |          |            |    
Z--Z    |    q~    |            |    **
        |    ]J    |            |    *a
\$\endgroup\$

3 Answers 3

6
\$\begingroup\$

Haskell, 97 96 95 bytes

  • -1 byte thanks to nimi
f s=all(r s/=)$foldr($)s<$>mapM(\_->[id,g,map g])[1..6]
g=r.foldr(zipWith(:))e
r=reverse
e=[]:e

Try it online!

Explanation

Ok, this is a bit mathy. It is well known that the group of rotations as described in the challenge (i.e. the group generated by rotations of 90 degrees around one of the three principal axes) is isomorphic to the symmetric group \$S_4\$, as described here. It is also well known that \$S_4\$ is generated by the elements \$(1\, 2\, 3\, 4),(1\, 3\, 2\, 4)\$.

What I am trying to say is this: fix two distinct axes, and call the 90 degrees rotations about those axes \$r_1\$ and \$r_2\$. Then we can generate all the rotations by repeatedly applying \$r_1\$ and \$r_2\$. A bit of bruteforce shows that we only need up to 6 applications of \$r_1\$ and \$r_2\$.

Thus we have the following algorithm: given a three-dimensional text \$s\$, we apply all possible rotations to \$s\$ and check whether any of those matches \$s\$ flipped along an axis. The rotations are generated by composing up to six 90 degrees rotations about two fixed axes.


g=r.foldr(zipWith(:))e
e=[]:e

g is a 90 degrees rotation of a 2D list (also works on 3D lists, as those can be seen as 2D lists of 1D lists). The foldr(zipWith(:))e is simply transpose from Data.List, but shorter.


f s=all(r s/=)$foldr($)s<$>mapM(\_->[id,g,map g])[1..6]

f checks whether its argument s is chiral.

  • mapM(\_->[id,g,map g])[1..6] generates a list of all sequences of length 6 whose elements are any of id (the identity transformation, i.e. do nothing), g (90 degrees rotation about an axis), map g (90 degrees rotations about a different axis).
  • given a list [f1,f2,...,f6] of transformations, foldr($)s computes f1(f2(...f6(s)...), and (r s/=) checks whether the result is equal to s reversed (i.e. a mirror image of s).

So what f does is check if any rotation of s matches its mirror image, returning False if it does and True otherwise.

\$\endgroup\$
0
2
\$\begingroup\$

APL (Dyalog Classic), 28 bytes

~⊂∘⌽∊(⊢∪2 0 1∘⍉¨∪⌽∘⍉⍤2¨)⍣≡∘⊂

Try it online!

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

K (ngn/k), 36 bytes

{^*({?x,((++:')'x),(|+:)'x}/,x)?,|x}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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