首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是静物吗?

是静物吗?
EN

Code Golf用户
提问于 2020-12-07 21:08:28
回答 4查看 3.8K关注 0票数 33

问题:

你会收到康威生命游戏格的(部分)。您的任务是输出它是否代表一个静止的生命(后续的世代与当前的网格相同),一个周期最多为4的振荡器(每第n代与当前的网格相同),还是其他的东西。

无限网格的其余部分由死细胞组成。

生命游戏的

规则(

)

如果一个死细胞紧邻三个活细胞,它就会活下来。否则它就死定了。如果一个活细胞在2个或3个活细胞旁边,它就会存活。否则它就死定了。

输入/输出格式

输入和输出可以通过任何合理的方法-活单元的位置列表,bools数组,一个或多个字符串,等等。你不能假设死细胞被从输入网格的边缘剥离。输出必须是任意三个一致的值(例如,静息: 0,振荡器:1,其他:2)。

对于周期为5或更长的振荡器,“振荡器”或“其他”的输出都是可以接受的。

示例

(1人活着,0人死亡)

代码语言:javascript
复制
INPUT          OUTPUT
0 0 0 0
0 1 1 0        Still-Life
0 1 1 0
0 0 0 0


INPUT          OUTPUT
1 1
1 0            Other


INPUT          OUTPUT
0 0 0 1 1 0
0 0 0 1 0 1    Still-Life
0 0 0 0 1 0


INPUT          OUTPUT
0 1 0
0 0 1          Other
0 1 0


INPUT          OUTPUT
0 1
0 1            Oscillator (period 2)
0 1


INPUT          OUTPUT
1 1 0 0
1 1 0 0        Oscillator (period 2)
0 0 1 1
0 0 1 1


INPUT                  OUTPUT
0 1 1 0 0 0 0 0
0 0 0 1 0 0 0 0        Oscillator (period 3)
1 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0
1 0 0 0 1 1 1 1
0 0 1 0 0 0 0 0


INPUT                 OUTPUT
1 1 0 0 0 1 1
0 1 0 1 0 1 0
0 1 1 0 1 1 0         Oscillator (period 4)
0 1 0 1 0 1 0
1 1 0 0 0 1 1


INPUT          OUTPUT
1 1 1
1 0 0          Other (This is a glider - the same shape will repeat, but it will be shifted)
0 1 0

这是密码-高尔夫- -每种语言中最短的答案获胜。

EN

回答 4

Code Golf用户

发布于 2020-12-07 23:00:54

JavaScript (ES6),191个字节

期望一个二进制矩阵。返回false (静物),true (振荡器)或0 (其他).

代码语言:javascript
复制
m=>(M=m=(h=a=>[v,v,v,v,...a,v,v,v,v])(v=m.map(h,v=0),v=v.map(_=>0)),g=k=>(m=m.map((r,y)=>r.map((v,x)=>m.reduce(n=>d?n>>(m[~-(y+--d/3)]||[])[x+d%3-1]:n,v*16+8,d=9)&1)))+''!=M?k&&g(k-1):k<4)(4)

在网上试试!

评论

助手函数h用4个前导值和4个尾随值v扩展数组a[]

代码语言:javascript
复制
h = a => [v, v, v, v, ...a, v, v, v, v]

我们使用它来扩展原始矩阵的水平和垂直。更新后的矩阵的副本保存在M中:

代码语言:javascript
复制
M = m = h(v = m.map(h, v = 0), v = v.map(_ => 0))

然后使用g调用主递归函数k = 4

代码语言:javascript
复制
g = k =>                         // k = counter
  ( m =                          // update m[]:
    m.map((r, y) =>              //   for each row r[] at position y in m[]:
      r.map((v, x) =>            //     for each value v at position x in r[]:
        m.reduce(n =>            //       for each entry in m[]:
          d ?                    //         if d is not equal to 0:
            n >> (               //           right-shift n if the cell at:
              m[~-(y + --d / 3)] //             Y = floor(y + --d / 3) - 1
              || []              //
            )[x + d % 3 - 1]     //             X = x + (d mod 3) - 1
                                 //           is set
          :                      //         else:
            n,                   //           leave n unchanged
          v * 16 + 8,            //         starting with n = v * 16 + 8
                                 //         (0b1000 if v = 0, or 0b11000 if v = 1)
          d = 9                  //         and d = 9
        )                        //       end of reduce()
        & 1                      //       isolate the least significant bit
      )                          //     end of inner map()
    )                            //   end of outer map()
  ) + '' != M ?                  // coerce m[] to a string; if it's not equal to M[]:
    k && g(k - 1)                //   unless k = 0, do a recursive call with k - 1
  :                              // else:
    k < 4                        //   test whether there was at least 2 iterations
票数 9
EN

Code Golf用户

发布于 2020-12-09 19:40:35

Wolfram语言(数学),65字节

代码语言:javascript
复制
⌊CellularAutomaton["GameOfLife",p=#~ArrayPad~4,4]~Count~p/2⌋&

在网上试试!

返回2表示静物,1返回振荡器,0返回其他.

票数 6
EN

Code Golf用户

发布于 2020-12-10 12:18:53

05AB1E,43 字节数

代码语言:javascript
复制
8Fø¾δ.ø}©4FÐ2Fø¾δ.ø}2F€ü3ø}OO·α567såD®Qˆ}¯Ù

@LuisMendo数学答案的启发,只是不太方便的建筑。矩阵的挑战并不是05AB1E的强项。

静态输出[1],振荡器输出[0,1],其他输出输出[0] .

在网上试试验证所有测试用例.

Explanation:

代码语言:javascript
复制
# Pad the input with four layers of 0s:
8F            # Loop 8 times:
  ø           #  Zip/transpose, swapping rows/columns
              #  (which uses the implicit input in the first iteration)
    δ         #  Loop over each row:
   ¾ .ø       #   And surround it with a leading and trailing 0
}©            # After the loop: store the padded input in variable `®` (without popping)

# Now we'll do a Game of Life cycle, four iterations in total:
4F            # Loop 4 times:
  D           #  Duplicate the current matrix
   2Fø¾δ.ø}   #  Pad it with a single layer of 0s similar as above
   2F         #  Loop 2 times:
     €        #   Map over each row:
      ü3      #    Create overlapping triplets of the current row
     ø        #   Zip/transpose; swapping rows/columns
   }          #  After this loop, we'll have a list of 3x3 blocks
    OO        #  Sum all values in a single 3x3 block together
      ·       #  Then double each sum
       α      #  Take the absolute difference (at the same positions) with the current
              #  matrix we've duplicated earlier
        567så #  Check for each value in this matrix if it's in the integer 567
              #  (1 if truthy; 0 if falsey)
  D           #  Duplicate the resulting matrix
   ®Q         #  Check if it's equal to matrix `®` (1 if truthy; 0 if falsey)
     ˆ        #  Pop and add this check to the global array
}             # After the loop:

# Create the result based on these four iterations:
¯             # Push the global array
 Ù            # Uniquify it
              # (after which the result is output implicitly)
票数 5
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/216087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档