Skip to main content
added 1363 characters in body
Source Link
Mr. Xcoder
  • 43k
  • 9
  • 87
  • 222

Explanation


q{@U9.nm+mc|g1aZksd2-MC.DR3d_dC,t –> Full program. Input: a list L from STDIN.

                               ,t –> Pair L with L without the first element.
                              C   –> Transpose.
       m                          –> Map over the list of pairs (2-element lists):
        +mc|g1aZksd2-MC.DR3d      –> The function to be mapped (variable: d):
                         R d          –> For each element of d ...
                       .D 3           –> ... Take its divmod by 3.
                      C               –> Tranpose.
                    -M                –> Reduce each by subtraction.
         m                            –> For each difference (variable: k):
            g1aZl                     –> Is |k| ≤ 1?
           |     sd                   –> If that's falsy, replace it by the sum of d.
          c        2                  –> Divide by 2.
        +                   _d    –> Append the reverse of d to the result of mapping.
     .n                           –> Flatten.
  @U9                             –> Take the intersection with (ℤ ∩ [0; 9)).
 {                                –> Deduplicate.
q                                 –> And check whether the result equals L.

Alternative approach for 34 bytes:

q{sI#I#+Fm+,hdcR2+MCd]edCtBK.DR3QK

Explanation


q{@U9.nm+mc|g1aZksd2-MC.DR3d_dC,t –> Full program. Input: a list L from STDIN.

                               ,t –> Pair L with L without the first element.
                              C   –> Transpose.
       m                          –> Map over the list of pairs (2-element lists):
        +mc|g1aZksd2-MC.DR3d      –> The function to be mapped (variable: d):
                         R d          –> For each element of d ...
                       .D 3           –> ... Take its divmod by 3.
                      C               –> Tranpose.
                    -M                –> Reduce each by subtraction.
         m                            –> For each difference (variable: k):
            g1aZl                     –> Is |k| ≤ 1?
           |     sd                   –> If that's falsy, replace it by the sum of d.
          c        2                  –> Divide by 2.
        +                   _d    –> Append the reverse of d to the result of mapping.
     .n                           –> Flatten.
  @U9                             –> Take the intersection with (ℤ ∩ [0; 9)).
 {                                –> Deduplicate.
q                                 –> And check whether the result equals L.

Alternative approach for 34 bytes:

q{sI#I#+Fm+,hdcR2+MCd]edCtBK.DR3QK
Source Link
Mr. Xcoder
  • 43k
  • 9
  • 87
  • 222

Pyth, 33 bytes

q{@U9.nm+mc|g1aZksd2-MC.DR3d_dC,t

Test suite.

Uses 0-based indexing.