12
\$\begingroup\$

In this challenge, your task is to detect (vertical) Skewer Symmetry. This means that one half of the pattern can be produced by mirroring the other half along a vertical axis, and then moving it vertically.

For example, the following pattern has skewer symmetry:

asdf    
jkl;fdsa
    ;lkj

Because if you start from the left half...

asdf               
jkl;

...then mirror it along a vertical axis...

    fdsa
    ;lkj

...then move it down by a character (filling the empty rows with spaces)...


    fdsa
    ;lkj

...you get the second half.

Rules:

  • You may assume the input is rectangle and has an even number of columns.
  • If the pattern itself exhibits reflection symmetry, it is considered skewer symmetry.
  • This is strict character by character symmetry, so [[ is considered symmetrical, but not [].
  • You should output truthy if the input has skewer symmetry, falsy otherwise.
  • Default I/O rules apply, standard loopholes are banned.

Test Cases

Truthy cases:

asdf    
jkl;fdsa
    ;lkj
asdffdsa
[ 


 [
  ba
abdc
cd

Falsy cases:

[ 
 ]
ab
ba
aa
a 
 a
a  a
b b 
ab  
  b 
   a
\$\endgroup\$
7
  • \$\begingroup\$ suggested test case: ["abfe","cdhg","efba","ghdc"]. i don't know what the result should be. \$\endgroup\$
    – ngn
    Commented Oct 4, 2019 at 13:49
  • 1
    \$\begingroup\$ Suggested test case: ["ab ", " b ", " a"] (where the columns are symmetric with the spaces stripped, but not with spaces present) \$\endgroup\$
    – user48543
    Commented Oct 4, 2019 at 15:16
  • 1
    \$\begingroup\$ @ngn This is similar to the ["ab", "ba"] test case, result should be falsy (it’s a symmetry+rotation, while skew symmetry is only symmetry+translation). \$\endgroup\$
    – Grimmy
    Commented Oct 4, 2019 at 18:12
  • 1
    \$\begingroup\$ @Grimy Formatting ate the array. It's supposed to be four columns, with 'a' in the first and last and 'b' in the second and third. So ["ab..", "..b.", "...a"], with the dots replaced with spaces. \$\endgroup\$
    – user48543
    Commented Oct 4, 2019 at 18:19
  • 1
    \$\begingroup\$ Suggested test case: [" ba","abdc","cd "] -> truthy (left part is moved downwards). May we assume, that the input contains only printable ASCII (' ' (space) to '~')? \$\endgroup\$
    – nimi
    Commented Oct 5, 2019 at 8:29

3 Answers 3

4
+200
\$\begingroup\$

APL (Dyalog Unicode), 44 34 bytesSBCS

crossed out 44 is still regular 44 ;(

Hats off to @Bubbler, @ngn and @TesselatingHeckler as they managed to shave some bytes off of my solution, more or less independently but coming up with the same simplifications.

{1∊(l↓⍵)⍷⍨a/⍨∨⌿' '≠a←⊖⍵↑⍨l←2÷⍨≢⍵}⍉

Try it online!

Now I'll explain how it works:

{                                   }⍉  ⍝ We define a monadic function that we apply right after transposing (⍉) the input [this means we will be operating on columns instead of rows]
                              2÷⍨≢⍵    ⍝ Start by finding how many  columns the input has (≢),split that in half (2÷⍨)
                            l←          ⍝   and assign that to l (l←) ...
                        ⍵↑⍨            ⍝ Using it right away to take (↑⍨) l columns from the original input ⍵
                       ⊖               ⍝   which we then reverse (⊖) [effectively flipping the columns upside down]
                     a←                 ⍝   and assign to a (a←)
                 ' '≠                   ⍝ We then compare each character in a to ' '
               ∨⌿                       ⍝   and we "interleave" the logical OR (∨) over the columns (⌿) [to find columns where at least one character is different from ' ']
           a/⍨                          ⍝   finally selecting those columns from a (a/⍨)
         ⍷⍨                             ⍝ Now we superimpose that piece of the original character matrix over all possible locations
   (l↓⍵)                               ⍝   of the columns of ⍵ that remain after we drop (↓) the first l columns [i.e. we take the second half of the columns]
 1∊                                    ⍝   and we look for matches by checking if there is a 1 (Truthy) in any position (∊)
\$\endgroup\$
3
  • \$\begingroup\$ Quick golf using transposed array gives 36 bytes. \$\endgroup\$
    – Bubbler
    Commented May 2, 2020 at 5:44
  • \$\begingroup\$ @Bubbler Quick for you ⍨ ahah, thanks for the help! \$\endgroup\$
    – RGS
    Commented May 2, 2020 at 11:25
  • \$\begingroup\$ 33: {1∊(w/⍨∨⌿' '≠w←⊖l↓⍵)⍷⍵↓⍨l←2÷⍨≢⍵}⍉ \$\endgroup\$
    – Adám
    Commented May 6, 2020 at 13:34
2
\$\begingroup\$

J, 34 23 38 bytes

1 e.[:(e.&:((-:@#{.])"1)|."1\.)"2],:|.

Try it online!

thanks to nimi for catching a bug

Will attempt to regolf the fixed version later.

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

Haskell, 137 135 133 bytes

f s=uncurry(#)$unzip$splitAt(div(length$s!!0)2)<$>s
a#b@(x:y)|e$last a++x=init a#y|e$last b++a!!0=b#a|r<-reverse<$>b=a==r
e=all(<'!')

Try it online!

\$\endgroup\$

Your Answer

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

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