21
\$\begingroup\$

The Idea

We've done matrix spirals before, and full rotations, and even diagonal rotations, but not, as far as I can find, snake rotations!

What is a snake rotation?

Imagine the rows of a matrix snaking back and forth, with dividers between them like the dividers of long queue:

    +--------------+
      1  2  3  4  5|
    +------------  |
    |10  9  8  7  6|
    |  +-----------+
    |11 12 13 14 15|
    +------------  |
     20 19 18 17 16|
    +--------------+

Now imagine rotating these items by 2. Each item advances, like people moving in a line, and the items at the end spill out and return to the beginning:

    +--------------+
-->  19 20  1  2  3|
    +------------  |
    | 8  7  6  5  4|
    |  +-----------+
    | 9 10 11 12 13|
    +------------  |
<--  18 17 16 15 14|
    +--------------+

If there are an odd number of rows it will exit from the right, but still wrap to the beginning. For example, here's a 3 rotation:

    +--------------+
      1  2  3  4  5|
    +------------  |
    |10  9  8  7  6|
    |  +-----------+
    |11 12 13 14 15
    +--------------+


    +--------------+
-->  13 14 15  1  2|
    +------------  |
    | 7  6  5  4  3|
    |  +-----------+
    | 8  9 10 11 12  -->
    +--------------+

A negative rotation will take you backwards. Here's a -2 rotation:

    +--------------+
<--   3  4  5  6  7|
    +------------  |
    |12 11 10  9  8|
    |  +-----------+
    |13 14 15  1  2  <--
    +--------------+

The Challenge

Your function or program will take 2 inputs, in any convenient format:

  • A matrix
  • A integer (positive or negative) indicating how many places to rotate it.

It will return:

  • The rotated matrix

Notes:

  • Code golf. Fewest bytes wins.
  • Matrixes need not be square, but will contain at least 2 rows and 2 columns
  • Positive integers will rotate row 1 toward the right
  • Negative integers will rotate row 1 toward the left
  • You may reverse the meaning of positive / negative rotation numbers, if convenient
  • The rotation number can be larger than the number of items. In that case, it will wrap. That is, it will be equivalent to the number modulo the number of items.
  • The matrix will contain only integers, but it may contain any integers, including repeats

Test Cases

Format:

  • Matrix
  • Rotation number
  • Expected return value

4 5
6 7

1

6 4
7 5

2  3  4  5
6  7  8  9
10 11 12 13

-3

5  9  8  7
12 11 10 6
13 2  3  4 

8 8 7 7
5 5 6 6

10

5 5 8 8
6 6 7 7
\$\endgroup\$
  • 4
    \$\begingroup\$ Reversing meaning of +/- is fine. I think the entrance should stay at the top left though. \$\endgroup\$ – Jonah Apr 14 at 1:24
  • 7
    \$\begingroup\$ This definitely needs an answer in Python. \$\endgroup\$ – 640KB Apr 14 at 13:47

12 Answers 12

7
\$\begingroup\$

Jelly, 10 bytes

UÐeẎṙṁ⁸UÐe

A dyadic Link accepting the marix on the left and the rotation integer on the right (uses the reverse meaning of positive / negative)

Try it online!

How?

UÐeẎṙṁ⁸UÐe - Link: matrix of integers, M; integer, R
 Ðe        - apply to even indices of M:
U          -   reverse each
   Ẏ       - tighten
    ṙ      - rotate left by R
     ṁ     - mould like:
      ⁸    -   chain's left argument, M
        Ðe - apply to even indices:
       U   -   reverse each
\$\endgroup\$
6
\$\begingroup\$

R, 121 110 101 bytes

function(m,n,o=t(m)){o[,j]=o[i<-nrow(o):1,j<-c(F,T)];o[(seq(o)+n-1)%%sum(1|o)+1]=o;o[,j]=o[i,j];t(o)}

Try it online!

Walkthrough

function(m,n) {           # Input: m - matrix, n - shift
  o <- t(m)               # Transpose the matrix, since R works in column-major order
                          # while our snake goes in row-major order
  i <- nrow(o):1          # Take row indices in reverse
  j <- c(F,T)             # Take even column indices (FALSE, TRUE, FALSE, TRUE, ...)
  o[,j] <- o[i,j]         # "Normalize" the matrix by reversing every second column
  o[(seq(o)+n-1) %%       # Perform the shift: add n-1 to indices,
    length(o)+1] <- o     # Modulo sequence length, and +1 again
  o[,j] <- o[i,j]         # Reverse even columns again to return to snake form
  t(o)                    # Transpose the matrix back to orginal shape and return
}
\$\endgroup\$
3
\$\begingroup\$

Python 3.8 (pre-releasSSSse), 119 bytes

lambda m,r,n=-1:[[m[(k:=(j+(s:=r+i)//w)%h)][::n**k][s%w]for i in range(w:=len(m[0]))][::n**j]for j in range(h:=len(m))]

An unnamed function accepting matrix, rotation which yields the new matrix.
Uses the opposite rotation sign.

Try it online!

How?

We set n=-1 upfront to save on parentheses later and take the matrix as m and the rotation as r.

A new matrix is constructed with the same dimensions as m - with a width of w (w:=len(m[0])) and a height of h (h:=len(m)).

Every other row of this matrix is reversed ([::n**j]).

The values are looked up by calculating their row and column in the original, m using the current elements row, i, and column, j...

We set s to r+i and k to (j+s//w)%h. k is the row of the original to access for our current element.

In order to easily access odd indexed rows from the right we reverse such rows before accessing their elements (with [:n**k]), this means the element of interest is at s%w.

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

J, 41 30 21 bytes

-11 bytes thanks to Jonah!

-9 bytes thanks to FrownyFrog & ngn !

$@]t@$(|.,@t=.|.@]/\)

Try it online!

Reversed +/-

\$\endgroup\$
  • 1
    \$\begingroup\$ 30 bytes, +/- not reversed, but still uses helper: $@]t@$(|.,@(t=.#\,`(|.@,)/.])) (Try it online!) \$\endgroup\$ – Jonah Apr 15 at 3:08
  • \$\begingroup\$ correction: +/- still reversed. \$\endgroup\$ – Jonah Apr 15 at 4:37
  • \$\begingroup\$ @Jonah Now that's J! I remember seeing you applying the same trick with the alternating reversal recently, but apparently have forgotten about it. Thank you! When trying &. I was loosing the left argument all the time, that's why I gave up. \$\endgroup\$ – Galen Ivanov Apr 15 at 6:36
  • 1
    \$\begingroup\$ 21 bytes, thx @ngn \$\endgroup\$ – FrownyFrog Apr 15 at 11:33
  • \$\begingroup\$ @FrownyFrog Wow, it's half the initial size now. I feel stupid... Thanks! \$\endgroup\$ – Galen Ivanov Apr 15 at 13:05
2
\$\begingroup\$

JavaScript (Node.js), 102 bytes

Takes input as (matrix)(integer). The meaning of the sign of the integer is inverted.

m=>n=>(g=m=>m.map(r=>r.sort(_=>~m,m=~m)))(m.map(r=>r.map(_=>a[(l+n++%l)%l]),l=(a=g(m).flat()).length))

Try it online!

Helper function

The helper function \$g\$ is used to 'snakify' or 'unsnakify' a matrix by reversing rows at odd indices.

g = m =>        // m[] = input matrix
  m.map(r =>    // for each row r[] in m[]:
    r.sort(_ => //   sort r[]:
      ~m,       //     using either 0 (don't reverse) or -1 (reverse)
      m = ~m    //     and toggling m before each iteration
                //     (on the 1st iteration: m[] is coerced to 0, so it yields -1)
    )           //   end of sort()
  )             // end of map()

Main function

m => n =>                    // m[] = matrix, n = integer
  g(                         // invoke g on the final result
    m.map(r =>               //   for each row r[] in m[]:
      r.map(_ =>             //     for each entry in r[]:
        a[(l + n++ % l) % l] //       get the rotated value from a[]; increment n
      ),                     //     end of inner map()
      l = (                  //     l is the length of a[]:
        a = g(m).flat()      //       a[] is the flatten result of g(m)
      ).length               //       (e.g. [[1,2],[3,4]] -> [[1,2],[4,3]] -> [1,2,4,3])
    )                        //   end of outer map()
  )                          // end of call to g
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 16 bytes

εNFR]˜²._¹gäεNFR

Try it online!

Thanks to Emigna for -5. Unfortunately, I can't see how to golf the redundant part out. :(

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

Charcoal, 36 bytes

FEθ⎇﹪κ²⮌ιιFι⊞υκIE⪪Eυ§υ⁻κηL§θ⁰⎇﹪κ²⮌ιι

Try it online! Link is to verbose version of code. Explanation:

Eθ⎇﹪κ²⮌ιι

Reverse alternate rows of the input.

F...Fι⊞υκ

Flatten the array.

Eυ§υ⁻κη

Rotate the flattened array.

⪪...L§θ⁰

Split the array back into rows.

E...⎇﹪κ²⮌ιι

Reverse alternate rows.

I...

Convert each entry to string and output in the default output format which is one number per line with rows double-spaced. (Formatting with a separator would cost the length of the separator.)

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

Pyth, 20 bytes

L.e_W%k2bbyclQ.>syQE

Try it online here.

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

Japt, 28 bytes

mÏ%2©XÔªX
c éV òUÎl
W©UªßV1V

Try it

Port of Arnauld's answer. The biggest challenge was creating a reusable function. In particular, there is a helper function to reverse every other row. The approach that I am taking is to make a recursive call and depending on whether a variable is set.

Transpiled JS:

// U: first input argument (matrix)
// m: map it through a function
U = U.m(function(X, Y, Z) {
  // if the row index is even, don't alter it
  // if the row index is odd, reverse it (w)
  return Y % 2 && X.w() || X
});
V = U
  // flatten the matrix
  .c()
  // shift by the amount specified in second argument
  .é(V)
  // partition back to matrix
  .ò(
    // the number of columns should be the same as input
    U.g().l()
  );
// if W is specified, return the result from the first line
W && U ||
  // otherwise, make a recursive call with the shifted matrix
  rp(V, 1, V)
\$\endgroup\$
1
\$\begingroup\$

Python 3, 94 bytes

lambda m,n:g(roll(g(m),n))
g=lambda b:[b[i][::(-1)**i]for i in r_[:len(b)]]
from numpy import*

Try it online!

Used the odd-row-reversal from Jonathan Allan's answer.

lambda m,n:g(roll(g(m),n))  #reverse odd rows, shift elements, then reverse odd rows again.
g=lambda b:[b[i][::(-1)**i] #reverse odd rows
    for i in r_[:len(b)]]   #r_[:x] = range(x)
from numpy import*          #roll() and r_[]
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Classic), 20 bytes

↑∘t⊢∘⍴⍴⌽∘∊∘(t←⊢∘⌽\↓)

Try it online!

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

C# (Visual C# Interactive Compiler), 141 bytes

a=>n=>{for(dynamic l=a.Length,w=a.GetLength(1),i=l,j,b=a.Clone();i-->0;)a[(j=(i+n%l+l)%l)/w,j/w%2<1?j%w:w-j%w-1]=b[i/w,i/w%2<1?i%w:w-i%w-1];}

Try it online!

-5 bytes total thanks to @someone!

Anonymous function that performs an in-place modification to the input matrix.

An single loop iterates over the cells. You can scan from top to bottom and left to right using the following formulas:

  • row=i/w
  • col=i%w

Where i is a loop counter and w is the number of columns. This is varies slightly when scanning in a snake pattern.

  • row=i/w
  • col=i%w (0th, 2nd, 4th, etc. row)
  • col=w-i%w-1 (1st, 3nd, 5th, etc. row)

Another thing to note is that the % in C# does not convert to a positive value like it does in some other languages. A couple extra bytes are needed to account for this.

// a: input matrix
// n: number of cells to rotate
a=>n=>{
  for(
    // l: total number of cells
    // w: number of columns
    // i: loop index
    // j: offset index
    // b: copy of input matrix
    dynamic
      l=a.Length,
      w=a.GetLength(1),
      i=l,j,
      b=a.Clone();
    // iterate from i down to 0
    i-->0;
  )
    // calculate the offset `j` and use
    // the above formulas to index
    // into `a` for setting a value
    a[
      (j=(i+n%l+l)%l)/w,
      j/w%2<1?j%w:w-j%w-1
    ]=
    // use the un-offset index `i` and
    // the above formulas to read a
    // value from the input matrix
    b[
      i/w,
      i/w%2<1?i%w:w-i%w-1
    ];
}
\$\endgroup\$
  • \$\begingroup\$ You can save 3 bytes by merging declarations with dynamic; comment too l. Try it online! \$\endgroup\$ – someone Apr 17 at 5:07
  • \$\begingroup\$ Nice :) That declaration can be moved into the loop as well. I tend to use var for golfing which doesn't let you declare a list of variables. Probably why I missed this. Good catch! \$\endgroup\$ – dana Apr 17 at 7:55
  • \$\begingroup\$ Get rid of y entirely to save 2 bytes: Try it online! \$\endgroup\$ – someone Apr 17 at 11:46
  • \$\begingroup\$ @someone - thanks! \$\endgroup\$ – dana Apr 18 at 0:56
  • \$\begingroup\$ TIO 135 with 1d array and width input. \$\endgroup\$ – someone May 1 at 3:50

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.