Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m8s
23 lines
1.0 KiB
APL
23 lines
1.0 KiB
APL
⍝ Conway's Game of Life — toroidal one-liner
|
||
⍝
|
||
⍝ The classic Roger Hui formulation:
|
||
⍝ life ← {⊃1 ⍵ ∨.∧ 3 4 = +/ +/ ¯1 0 1 ∘.⊖ ¯1 0 1 ⌽¨ ⊂⍵}
|
||
⍝
|
||
⍝ Read right-to-left:
|
||
⍝ ⊂⍵ : enclose the board (so it's a single scalar item)
|
||
⍝ ¯1 0 1 ⌽¨ ⊂⍵ : produce 3 horizontally-shifted copies
|
||
⍝ ¯1 0 1 ∘.⊖ … : outer-product with vertical shifts → 3×3 = 9 shifts
|
||
⍝ +/ +/ … : sum the 9 boards element-wise → neighbor-count + self
|
||
⍝ 3 4 = … : boolean — count is exactly 3 or exactly 4
|
||
⍝ 1 ⍵ ∨.∧ … : "alive next" iff (count=3) or (alive AND count=4)
|
||
⍝ ⊃ … : disclose back to a 2D board
|
||
⍝
|
||
⍝ Rules in plain language:
|
||
⍝ - dead cell + 3 live neighbors → born
|
||
⍝ - live cell + 2 or 3 live neighbors → survives
|
||
⍝ - all else → dies
|
||
⍝
|
||
⍝ Toroidal: edges wrap (rotate is cyclic).
|
||
|
||
life ← {⊃1 ⍵ ∨.∧ 3 4 = +/ +/ ¯1 0 1 ∘.⊖ ¯1 0 1 ⌽¨ ⊂⍵}
|