Slither Like a Snake

21

1

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

Jonah

Posted 2019-04-14T00:23:42.827

Reputation: 8 729

4Reversing meaning of +/- is fine. I think the entrance should stay at the top left though. – Jonah – 2019-04-14T01:24:55.577

7This definitely needs an answer in Python. – 640KB – 2019-04-14T13:47:46.693

Answers

7

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

Jonathan Allan

Posted 2019-04-14T00:23:42.827

Reputation: 67 804

6

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
}

Kirill L.

Posted 2019-04-14T00:23:42.827

Reputation: 6 693

3

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.

Jonathan Allan

Posted 2019-04-14T00:23:42.827

Reputation: 67 804

3

J, 41 30 21 bytes

-11 bytes thanks to Jonah!

-9 bytes thanks to FrownyFrog & ngn !

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

Try it online!

Reversed +/-

Galen Ivanov

Posted 2019-04-14T00:23:42.827

Reputation: 13 815

1

30 bytes, +/- not reversed, but still uses helper: $@]t@$(|.,@(t=.#\,`(|.@,)/.])) (Try it online!)

– Jonah – 2019-04-15T03:08:22.077

correction: +/- still reversed. – Jonah – 2019-04-15T04:37:57.800

@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. – Galen Ivanov – 2019-04-15T06:36:28.447

121 bytes, thx @ngn – FrownyFrog – 2019-04-15T11:33:10.117

@FrownyFrog Wow, it's half the initial size now. I feel stupid... Thanks! – Galen Ivanov – 2019-04-15T13:05:28.510

@FrownyFrog very nice. i knew there were more bytes to be shaved off in the t helper, but you just Dundee'd me.

– Jonah – 2019-04-15T13:56:31.737

2

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

Arnauld

Posted 2019-04-14T00:23:42.827

Reputation: 111 334

2

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. :(

Erik the Outgolfer

Posted 2019-04-14T00:23:42.827

Reputation: 38 134

1

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

Neil

Posted 2019-04-14T00:23:42.827

Reputation: 95 035

1

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
    ];
}

dana

Posted 2019-04-14T00:23:42.827

Reputation: 2 541

You can save 3 bytes by merging declarations with dynamic; comment too l. Try it online!

– my pronoun is monicareinstate – 2019-04-17T05:07:41.337

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! – dana – 2019-04-17T07:55:34.330

Get rid of y entirely to save 2 bytes: Try it online!

– my pronoun is monicareinstate – 2019-04-17T11:46:20.380

@someone - thanks! – dana – 2019-04-18T00:56:52.320

TIO 135 with 1d array and width input. – my pronoun is monicareinstate – 2019-05-01T03:50:04.560

@someone - Interesting approach. Is that format valid? You can get it a lot shorter :) – dana – 2019-05-01T04:39:21.200

@someone - 105

– dana – 2019-05-01T04:40:03.407

I believe that doesn't work correctly, every second row is reversed. – my pronoun is monicareinstate – 2019-05-01T09:42:23.400

Ah, your right! The flat array opens up options though. Hadn't thought about that. – dana – 2019-05-01T12:07:04.520

I think ToList() can be omitted safely (returning a IEnumerable<int>). The declarations can also be moved for 97 bytes, a pastebin link to a TIO link, but I really can't seem to get the math to fix this right.

– my pronoun is monicareinstate – 2019-05-01T12:12:14.653

1

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)

dana

Posted 2019-04-14T00:23:42.827

Reputation: 2 541

1

Pyth, 20 bytes

L.e_W%k2bbyclQ.>syQE

Try it online here.

Sok

Posted 2019-04-14T00:23:42.827

Reputation: 5 592

1

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_[]

attinat

Posted 2019-04-14T00:23:42.827

Reputation: 3 495

1

APL (Dyalog Classic), 20 bytes

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

Try it online!

ngn

Posted 2019-04-14T00:23:42.827

Reputation: 11 449