| Bytes | Lang | Time | Link |
|---|---|---|---|
| 037 | J | 240726T160517Z | noodle p |
| 029 | Uiua | 240725T220351Z | noodle p |
| 205 | R | 190223T011111Z | ASCII-on |
| 123 | Wolfram Language Mathematica | 190127T015007Z | lirtosia |
| 159 | Python 2 | 190122T140821Z | TFeld |
| 039 | APL+WIN | 190122T144917Z | Graham |
| 017 | Jelly | 190122T132743Z | Jonathan |
| 039 | 05AB1E | 190122T095632Z | Kevin Cr |
J, 40 38 37 bytes
G=:{{|.~"p.u@1|.!.0#.}}
-G&.(1&|:)+G]
Explanation:
The operation of rotating an array by its rows read as binary occurs twice if we consider the column-rotation to just be rotating the rows under transposing each matrix, so we extract it to a function G. However, in the row-rotation we shift the binary by -1 and in the column rotation we shift it by 1, so G has to be an explicit adverb in order to pass that third argument + or -.
G=:{{|.~"p.u@1|.!.0#.}} Define the adverb G. u is + or -
|.~"p. Rotate the rows of the left array by
#. the right array's rows read as binary,
|.!.0 shifted, filling with zeroes, by
u@1 u called on 1, so either 1 or _1
The second line is the function submission. It calls G twice, first under transposition of the matrices to rotate the rows, shifting down (-G&.(1&|:) and then using this as the array to rotate the columns of, shifting up (+G]).
Uiua, 31 29 bytes
≡≡↻⊙≡⍜∩⍉≡↻∩⬚0↻1⊙¯1∩°⋯∩≡≡⇌⟜≡⍉.
Explanation:
∩°⋯∩≡≡⇌⟜≡⍉.
Convert the rows and the columns to binary. ⋯ (bits) uses little-endian binary, so the rows and the columns have to be reversed first.
∩⬚0↻1⊙¯1
Rotate the array of the columns up by one and the array of the rows down by one, filling with zeroes instead of cycling.
⊙≡⍜∩⍉≡↻
Rotate the columns.
≡≡↻
Rotate the rows.
R, 226 216 205 bytes
-21 bytes thanks to digEmAll
function(a,L=`for`){d=dim(b<-a)
r=function(a,n,l=sum(a|1))a[(1:l+sum(n*2^(sum(n|1):1-1))-1)%%l+1]
L(i,I<-2:d[3],L(j,1:d,b[j,,i]<-r(b[j,,i],a[j,,i-1])))
L(i,I-1,L(k,1:d[2],b[,k,i]<-r(b[,k,i],a[,k,i+1])))
b}
Wolfram Language (Mathematica), 138 131 125 123 bytes
t=Map@Thread
m=MapThread[r=RotateLeft,#,2]&
b=(a=ArrayPad)[Map@Fold[#+##&]/@#,1]~r~#2~a~-1&
g=m@{t@m@{t@#,t@#~b~-1},#~b~1}&
Map[Thread]is equivalent toTranspose[a, {1,3,2}], which transposes the columns and rows.Fold[#+##&]is shorter thanIntegerDigits[#,2]for converting from binary.
Python 2, 220 211 209 185 176 174 164 161 159 bytes
lambda m:map(R,z(map(R,z(m,['']+[z(*l)for l in m])),m[1:]+['']))
R=lambda(l,L):map(lambda r,i:r[i:]+r[:i or 0],z(*l),[int(`b`[1::3],2)%len(b)for b in L])
z=zip
-2 bytes, thanks to Jonathan Allan
APL+WIN, 53 39 bytes
Many thanks to Adám for saving 14 bytes
(1 0↓⍉2⊥⍉m⍪0)⌽(¯1 0↓2⊥2 1 3⍉0⍪m)⊖[2]m←⎕
Try it online! Courtesy of Dyalog Classic
Prompts for input of a 3d array of the form:
4 2 3⍴1 0 1 1 0 0 1 0 1 0 1 1 0 1 1 1 1 1 1 1 0 1 1 1
which yields:
1 0 1
1 0 0
1 0 1
0 1 1
0 1 1
1 1 1
1 1 0
1 1 1
Explanation:
m←⎕ Prompt for input
(¯1 0↓2⊥2 1 3⍉0⍪m) Calculate column rotations
(1 0↓⍉2⊥⍉m⍪0) Calculate row rotations
(...)⌽(...)⊖[2]m Apply column and row rotation and output resulting 3d array:
1 1 0
1 0 0
0 0 1
1 1 1
0 1 1
1 1 1
1 1 1
1 1 0
Jelly, 18 17 bytes
ṙ""Ḅ}
Z€çŻṖ$$Z€çḊ
How?
ṙ""Ḅ} - Link 1, rotation helper: 3d matrix to rotate, 3d matrix of rotation instructions
} - use the right argument for:
Ḅ - un-binary (vectorises) - get the rotation amounts as a 2d matrix
" - zip with:
" - zip with:
ṙ - rotate (the current row) left by (the current amount)
Z€çŻṖ$ $Z€çḊ - Main Link: 3d matrix, M
Z€ - transpose €ach (layer of M)
$ - last two links as a monad:
$ - last two links as a monad:
Ż - prepend a zero
Ṗ - pop (i.e. remove the tail)
ç - call the last Link as a dyad (i.e. f(Z€ result, ŻṖ$ result) )
Z€ - transpose €ach (layer of that)
Ḋ - dequeue (i.e. remove the head layer of M)
ç - call the last Link as a dyad (i.e. f(Z€çŻṖ$$Z€ result, Ḋ result) )
Note: $$ (or possibly $$ ... $$?) seems to mess up the code-block formatting (but only once posted, not in the preview), so I added a space to make my life easier.
05AB1E, 41 39 bytes
εNĀiø¹N<èøJC‚øε`._}ø}N¹g<Êi¹N>èJC‚øε`._
This feels way too long.. Can definitely be golfed some more.
Try it online or verify all test cases.
Explanation:
ε # Map each layer in the (implicit) input to:
# (`N` is the layer-index of this map)
NĀi # If it is not the first layer:
ø # Zip/transpose the current layer; swapping rows/columns
¹N<è # Get the `N-1`'th layer of the input
ø # Zip/transpose; swapping rows/columns
J # Join all inner lists (the columns) together
C # And convert it from binary to integer
‚ # Pair it with the current layer's columns we're mapping
ø # Zip/transpose; to pair each integer with a layer's columns
ε } # Map over these pairs:
` # Push both values of the pair separately to the stack
._ # Rotate the column the integer amount of times
ø # Zip/transpose the rows/columns of the current layer back
} # Close the if-statement
N¹g<Êi # If this is not the last layer (layer-index-1 != amount_of_layers):
¹N>è # Get the `N+1`'th layer of the input
J # Join all inner lists (the rows) together
C # And convert it from binary to integer
‚ # Pair it with the current layer's rows we're mapping
ø # Zip/transpose; to pair each integer with a layer's rows
ε # Map over these pairs:
` # Push both values of the pair separately to the stack
._ # Rotate the row the integer amount of times
# (implicitly output the result after the layer-mapping is done)