二进制自转


13

给定一个二进制3D数组,对于每一层,按其上一层的各列的二进制编码所示,将其每一列循环向上旋转许多步,然后按如下所示将其每一行循环左旋转许多步下方图层的行的二进制编码。

总会有至少三层。顶层的列和底层的行不应旋转。

演练

让我们从小的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]]]

第一步是评估每一层的列和行以二进制编码的数字:

     3 0 2
5 [[[1,0,1],
4   [1,0,0]],

     2 1 3
5  [[1,0,1],
3   [0,1,1]],

     1 3 3
3  [[0,1,1],
7   [1,1,1]],

     3 3 1
6  [[1,1,0],
7   [1,1,1]]]

第一层[[1,0,1],[1,0,0]]不会旋转其列,但其行将分别向左循环5步和3步,从而变为[[1,1,0],[1,0,0]]
 第二层[[1,0,1],[0,1,1]]的列将分别循环向上旋转3、0和2步,给出[[0,0,1],[1,1,1]],然后将行分别向左循环旋转3和7步,没有可见变化。向上旋转2、1、3步
 的第三层[[0,1,1],[1,1,1]]保持不变,向左旋转6、7步也没有任何作用。
 最后,[[1,1,0],[1,1,1]]向上旋转了1、3和3步的第四层是[[1,1,1],[1,1,0]],但是之后的行不再旋转,因为它是最后一层。
 再次将所有图层放在一起,便得到了二进制自旋转3D数组:

[[[1,1,0],
  [1,0,0]],

 [[0,0,1],
  [1,1,1]],

 [[0,1,1],
  [1,1,1]],

 [[1,1,1],
  [1,1,0]]]

示例案例:

[[[1,0,1],[1,0,0]],[[1,0,1],[0,1,1]],[[0,1,1],[1,1,1]],[[1,1,0],[1,1,1]]]
[[[1,1,0],[1,0,0]],[[0,0,1],[1,1,1]],[[0,1,1],[1,1,1]],[[1,1,1],[1,1,0]]]

[[[1]],[[1]],[[0]]]
[[[1]],[[1]],[[0]]]

[[[1,0,1],[1,0,1],[1,0,1]],[[0,0,1],[0,0,1],[0,0,1]],[[1,0,0],[1,0,1],[0,0,1]]]
[[[0,1,1],[0,1,1],[0,1,1]],[[0,1,0],[1,0,0],[0,1,0]],[[1,0,1],[1,0,1],[0,0,0]]]

Answers:


3

果冻 18  17 字节

ṙ""Ḅ}
Z€çŻṖ$$Z€çḊ

在线尝试!

怎么样?

ṙ""Ḅ} - 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) )

注意:($$或可能是$$ ... $$?)似乎弄乱了代码块的格式(但仅发布一次,而不是在预览中),所以我添加了一个空格,使我的生活更轻松。


3

Python 2中220 211 209 185 176 174个 164 161 159字节

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个字节,感谢Jonathan Allan


由于您None在旋转切片时会处理,所以我相信这两个罐子都['0']可以变成[[]]
乔纳森·艾伦,

@JonathanAllan谢谢:)
TF

2

APL + WIN,53 39字节

非常感谢Adám节省了14个字节

(1 0↓⍉2⊥⍉m⍪0)⌽(¯1 0↓2⊥2 1 3⍉0⍪m)⊖[2]m←⎕

在线尝试!由Dyalog Classic提供

提示输入以下形式的3d数组:

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

产生:

1 0 1
1 0 0

1 0 1
0 1 1

0 1 1
1 1 1

1 1 0
1 1 1

说明:

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

而不是封装和使用¨,只需一次处理整个数组。在线尝试!
亚当

@Adám非常感谢。我不知道为什么我会过分考虑这条路线,然后走嵌套路线:(变老了?
Graham

2

[R 226个 216 205字节

-21字节归功于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}

在线尝试!


1

05AB1E41 39 字节

εNĀiø¹N<èøJC‚øε`._}ø}N¹g<Êi¹N>èJC‚øε`._

这感觉太长了。绝对可以打更多的高尔夫球。

在线尝试验证所有测试用例

说明:

ε                    # 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¹gi              #  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)

0

Wolfram语言(数学)138个 131 125 123字节

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]等价于Transpose[a, {1,3,2}],它转置列和行。
  • Fold[#+##&]IntegerDigits[#,2]从二进制转换要短。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.