首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生命的游戏,但在4-8-8平局中

生命的游戏,但在4-8-8平局中
EN

Code Golf用户
提问于 2021-09-15 23:40:46
回答 5查看 2K关注 0票数 10

背景

4-8-8瓦看起来如下所示:

为了应对这一挑战,我们采用了上面所示的瓷砖的方向。在通俗易懂的英语单词中,我们把瓷砖作为方格和八角形的矩形网格,左上角是八角形。

为了在这个平铺上定义一个生命游戏,我们使用了一个简化的邻域定义:B是A的近邻,如果两个单元共享一个边。一个八角形最多有8个邻居,一个正方形最多有4个邻居。我们假设给定网格外的细胞根本不存在(或者,作为所有死亡的硬连线)。

除了网格差异之外,我们使用“生命游戏规则”如下:

  • 如果一个小区正好有三个邻居,那么它就会被打开,否则就会被关闭。
  • 如果一个单元格正好有两个或三个邻接,它就会保持开着,否则就会关闭。

挑战

在上面定义的4-8-8块上,给出一个矩形网格,计算“生命游戏”中的下一代。

on和off单元可以分别用两个不同的、一致的值表示。矩形网格可以以您的语言支持的任何适当格式表示。输出格式不需要与输入格式相同。可以假定网格至少有两行和两列。

适用标准的密码-高尔夫规则。以字节为单位的最短代码获胜。

测试用例

使用这个Python程序生成。

为了简单起见,网格被表示为零(off)和1 (on)的列表。有些测试用例有多个步骤,这意味着如果程序被赋予了第一个状态,它应该产生第二个,如果是第二个,它应该产生第三个,等等。

代码语言:javascript
复制
[[1, 1],
 [1, 1]] -> unchanged

[[1, 1],
 [0, 1]] -> unchanged

[[1, 1],
 [1, 0]] ->
[[1, 0],
 [0, 1]] ->
[[0, 0],
 [0, 0]]

[[0, 0, 0],
 [1, 1, 1],
 [0, 0, 0]], ->
[[0, 0, 0],
 [0, 1, 0],
 [0, 0, 0]]

[[0, 0, 0, 0, 0],
 [0, 1, 1, 1, 0],
 [0, 0, 0, 0, 0]] ->
[[0, 0, 1, 0, 0],
 [0, 0, 1, 0, 0],
 [0, 0, 1, 0, 0]] -> back to first

[[1, 0, 0, 1, 1],
 [1, 1, 0, 1, 0],
 [0, 0, 1, 0, 0],
 [0, 1, 0, 1, 1],
 [1, 1, 0, 0, 1]] ->
[[1, 0, 1, 1, 1],
 [1, 1, 1, 1, 0],
 [1, 1, 0, 1, 1],
 [0, 1, 1, 1, 1],
 [1, 1, 1, 0, 1]] ->
[[1, 1, 0, 1, 1],
 [1, 0, 1, 0, 1],
 [0, 1, 0, 1, 0],
 [1, 0, 1, 0, 1],
 [1, 1, 0, 1, 1]] ->
[[1, 0, 1, 0, 1],
 [0, 0, 0, 0, 0],
 [1, 0, 0, 0, 1],
 [0, 0, 0, 0, 0],
 [1, 0, 1, 0, 1]] ->
[[0, 0, 0, 0, 0],
 [0, 1, 0, 1, 0],
 [0, 0, 0, 0, 0],
 [0, 1, 0, 1, 0],
 [0, 0, 0, 0, 0]] -> all zeros
EN

回答 5

Code Golf用户

发布于 2021-09-16 02:45:31

JavaScript,99字节

代码语言:javascript
复制
a=>a.map((r,y)=>r.map((c,x)=>((g=i=>i--&&((i%2|x+~y&i!=4)&a[~-(y+i/3)]?.[x+i%3-1])+g(i))(9)|c)==3))
代码语言:javascript
复制
f=

a=>a.map((r,y)=>r.map((c,x)=>((g=i=>i--&&((i%2|x+~y&i!=4)&a[~-(y+i/3)]?.[x+i%3-1])+g(i))(9)|c)==3))

print = (a, contain) => {
  let t = contain.appendChild(document.createElement('div'));
  t.className = 'grid';
  a.map(r => {
    let tr = t.appendChild(document.createElement('div'));
    tr.className = 'row';
    r.map(c => {
      tr.appendChild(document.createElement('div')).className = c ? 'on cell' : 'off cell';
    });
  });
}

test = (a, n, contain) => {
  contain ??= document.body.appendChild(document.createElement('div'));
  contain.className = 'testcase';
  print(a, contain);
  if (n) test(f(a), n - 1, contain);
};

test([[true, true],
 [true, true]], 1);

test([[true, true],
 [false, true]], 1);

test([[true, true],
 [true, false]], 2);

test([[false, false, false],
 [true, true, true],
 [false, false, false]], 1);

test([[false, false, false, false, false],
 [false, true, true, true, false],
 [false, false, false, false, false]], 2);

test([[true, false, false, true, true],
 [true, true, false, true, false],
 [false, false, true, false, false],
 [false, true, false, true, true],
 [true, true, false, false, true]], 5);
代码语言:javascript
复制
body { background: #ccc; }
.grid { display: flex; flex-direction: column; padding: 10px; }
.grid + .grid { margin-top: 10px; }
.row { display: flex; }
.cell { width: 23px; height: 23px; position: relative; padding: 0; }
.on { --cell-color: white; }
.off { --cell-color: black; }
.cell::before { content: " "; display: block; position: absolute; }
.cell::before {
  width: 16px; height: 16px;
  top: 4px; left: 4px;
  background-image:
    linear-gradient(to bottom, var(--cell-color), var(--cell-color)),
    linear-gradient(to right, gray, gray);
  background-position: 1px 1px, top left;
  background-repeat: no-repeat;
  background-size: 14px 14px, 16px 16px;
}
.row:nth-child(2n) .cell:nth-child(2n)::before,
.row:nth-child(2n+1) .cell:nth-child(2n+1)::before {
  width: 34px; height: 34px;
  top: -5px; left: -5px;
  background-image:
    linear-gradient(to bottom, gray 0, gray 1px, transparent 1px, transparent 33px, gray 33px, gray 34px),
    linear-gradient(to right, gray 0, gray 1px, transparent 1px, transparent 33px, gray 33px, gray 34px),
    linear-gradient( 135deg, transparent 0, transparent 7px, gray 7px, gray 8px, var(--cell-color) 8px, var(--cell-color) 20px),
    linear-gradient(-135deg, transparent 0, transparent 7px, gray 7px, gray 8px, var(--cell-color) 8px, var(--cell-color) 20px),
    linear-gradient(  45deg, transparent 0, transparent 7px, gray 7px, gray 8px, var(--cell-color) 8px, var(--cell-color) 20px),
    linear-gradient( -45deg, transparent 0, transparent 7px, gray 7px, gray 8px, var(--cell-color) 8px, var(--cell-color) 20px);
  background-position: top center, left center, top left, top right, bottom left, bottom right;
  background-repeat: no-repeat;
  background-size: 16px 34px, 34px 16px, 20px 20px, 20px 20px, 20px 20px, 20px 20px;
}
.testcase { padding: 10px; }
.testcase + .testcase { border-top: 1px solid gray; }
票数 5
EN

Code Golf用户

发布于 2021-09-16 00:49:49

JavaScript (ES6),109个字节

代码语言:javascript
复制
a=>a.map((r,y)=>r.map((n,x)=>[...'0124689',10].map(i=>n+=x-~y&1|i&5&&(a[y+~-(i/4)]||0)[x+i%4-1]<<1)|n>4&n<8))

在网上试试!

票数 3
EN

Code Golf用户

发布于 2021-09-16 00:01:01

JavaScript (ES2020),139个字节

代码语言:javascript
复制
x=>x.map((y,i)=>y.map((v,j)=>(v+=(g=(a,b)=>x[i+a]?.[j+b]*2|0)(0,1)+g(1,0)+g(n=-1,0)+g(0,n)+(i+j+1)%2*(g(1,1)+g(1,n)+g(n,1)+g(n,n)))>4&v<8))

在网上试试! (Link使用142个字节版本,因为TIO不支持ES2020 ?.操作符)

解释

代码语言:javascript
复制
x=>x.map((y,i)=>y.map((v,j)=> // Map over each element of the grid:
  (v+=                          // Add to the value of the cell (0 or 1):
    (g=(a,b)=>x[i+a]?.[j+b]*2|0)  // Define a function g to get neighbor
                                  // values; 2 for alive, 0 for dead.
        (0,1)+g(1,0)              // Sum the neighbor values for the cells
      +g(n=-1,0)+g(0,n)           // above, below, left, and right.
      +                           // Add the product of:
        (i+j+1)%2                  // 1 for octagons, 0 for squares.
        *(g(1,1)+g(1,n)             // The sum of the values for the
         +g(n,1)+g(n,n))            // diagonal neighbors.
  )>4&v<8                       // 1 if the sum is >4 and <8, 0 otherwise.
))                              // This is true for:
                                // - 5 (1 (alive cell) + 4 (2 alive neighbors))
                                // - 6 (0 (dead cell) + 6 (3 alive neighbors))
                                // - 7 (1 (alive cell) + 6 (3 alive neighbors))
票数 2
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

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

复制
相关文章

相似问题

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