Slither Like a Snake
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
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
}
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
.