我们面临的挑战是找到3D生命游戏(示例)的最短实现。以下是规则:
细胞(在这种情况下,立方体)只有一个或更少的邻居死亡,好像孤独。
如果一个空细胞周围正好有5个细胞,它们就会繁殖并填满它。
如果一个牢房有8个或更多的邻居,它就会因过度拥挤而死亡。
至少制作一个10x10x10,其中各层分别输出如下:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 X 0 0 X 0 0 0 0 0
0 0 X X X 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0当然,图形三维模拟也是可以接受的。
起始位置可能是硬编码的,但如果更改为任何起始位置,则必须工作。它必须能够计算出任意数量的世代,并且用户必须能够手动请求下一代。
字符中最短的代码获胜!
我为任何(多维数据集)大小创建了自己的实现:http://jensrenders.site88.net/life3D.htm --您可以使用它进行测试,并且可以将代码建立在我的基础上,尽管我没有对它进行评论。
发布于 2014-03-22 21:57:14
g=CellularAutomaton[{(l=Flatten@#;c=l[[14]];n=Total@Drop[l,{14}];Which[n<2||n>7,0,n==5||c==1,1,0<1,0])&,{},{1,1,1}},##]&当然不是胜利的竞争者,但这不是我的本意。此外,这可能会被大大降低,只要计算出规则号码。我真的很想写一个可视化的东西(尽管我确信已经有很多了)。因此,我们开始):
animateGol3d[size_, i_, n_] :=
ListAnimate[
Graphics3D[
Cuboid /@ Position[#, 1],
PlotRange -> {{0, size}, {0, size}, {0, size}} + 1
] & /@ g[i, n]
];在对一系列初始条件进行了实验之后,我得到了如下内容:

这是一个网格大小为20x20x20的。这需要几秒钟的时间来模拟和渲染:

顺便说一下,这是一个周期性的边界条件。
发布于 2014-02-28 23:04:55
花了我一段时间,但我得到了46个字符:
{(5=m)∨⍵∧3>|5.5-m←⊃+/,i∘.⌽i∘.⊖(i←2-⍳3)⌽[2]¨⊂⍵}这是一个函数,它接受任意大小的布尔三维矩阵,并根据给定的规则计算下一代。边界条件没有指定,所以我选择绕着另一边,就像在环形空间中一样。
{ ⊂⍵} Take the argument matrix and enclose it in a scalar
(i←2-⍳3) Prepare an array with values -1 0 1 and call it i
⌽[2]¨ Shift the matrix along the 2nd dim. by each of -1 0 1
i∘.⊖ Then for each result do the same along the 1st dimension
i∘.⌽ And for each result again along the 3rd dimension
m←⊃+/, Sum element-wise all 27 shifted matrices and call it m中间结果m是一个与原始矩阵形状相同的矩阵,它计算每个单元在其3×3×3邻域中的活细胞数,包括其本身。然后:
|5.5-m For each element (x) in m, take its distance from 5.5
⍵∧3> If that distance is <3 (which means 3≤x≤8) and the original cell was 1,
(5=m)∨ or if the element of m is 5, then the next generation cell will be 1.定义一个随机的4×4×4矩阵,约为1/3单元,计算其第1代和第2代。正面的⊂[2 3]只是水平打印平面而不是垂直打印的技巧:
⊂[2 3] m←1=?4 4 4⍴3
1 0 0 0 1 0 1 0 1 0 1 0 0 0 0 1
1 1 0 0 0 0 0 0 0 0 0 1 1 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
1 1 0 0 0 0 0 1 1 0 0 1 0 0 1 0
⊂[2 3] {(5=m)∨⍵∧3>|5.5-m←⊃+/,i∘.⌽i∘.⊖(i←2-⍳3)⌽[2]¨⊂⍵} m
0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
1 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0
⊂[2 3] {(5=m)∨⍵∧3>|5.5-m←⊃+/,i∘.⌽i∘.⊖(i←2-⍳3)⌽[2]¨⊂⍵}⍣2⊢ m
0 0 1 0 1 0 1 0 1 0 1 0 0 0 0 0
1 0 1 0 0 0 1 1 0 0 0 0 1 0 1 0
1 0 0 0 1 1 0 0 0 0 1 0 1 0 1 0
1 1 1 0 1 0 0 1 1 0 1 0 0 0 1 0 https://codegolf.stackexchange.com/questions/22430
复制相似问题