你会收到康威生命游戏格的(部分)。您的任务是输出它是否代表一个静止的生命(后续的世代与当前的网格相同),一个周期最多为4的振荡器(每第n代与当前的网格相同),还是其他的东西。
无限网格的其余部分由死细胞组成。
生命游戏的
)
如果一个死细胞紧邻三个活细胞,它就会活下来。否则它就死定了。如果一个活细胞在2个或3个活细胞旁边,它就会存活。否则它就死定了。
输入和输出可以通过任何合理的方法-活单元的位置列表,bools数组,一个或多个字符串,等等。你不能假设死细胞被从输入网格的边缘剥离。输出必须是任意三个一致的值(例如,静息: 0,振荡器:1,其他:2)。
对于周期为5或更长的振荡器,“振荡器”或“其他”的输出都是可以接受的。
(1人活着,0人死亡)
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这是密码-高尔夫- -每种语言中最短的答案获胜。
发布于 2020-12-07 23:00:54
期望一个二进制矩阵。返回false (静物),true (振荡器)或0 (其他).
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[]:
h = a => [v, v, v, v, ...a, v, v, v, v]我们使用它来扩展原始矩阵的水平和垂直。更新后的矩阵的副本保存在M中:
M = m = h(v = m.map(h, v = 0), v = v.map(_ => 0))然后使用g调用主递归函数k = 4:
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发布于 2020-12-09 19:40:35
⌊CellularAutomaton["GameOfLife",p=#~ArrayPad~4,4]~Count~p/2⌋&返回2表示静物,1返回振荡器,0返回其他.
发布于 2020-12-10 12:18:53
8Fø¾δ.ø}©4FÐ2Fø¾δ.ø}2F€ü3ø}OO·α567såD®Qˆ}¯Ù受@LuisMendo数学答案的启发,只是不太方便的建筑。矩阵的挑战并不是05AB1E的强项。
静态输出[1],振荡器输出[0,1],其他输出输出[0] .
# 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)https://codegolf.stackexchange.com/questions/216087
复制相似问题