I have the following code which produces successive generations of Conway's Game of Life:
life =: 3 : '+./ (>(($ y) $ 1);y) *. > =&(+/ (>,{ ;~(1 0 _1))|. y) each 3 4'
Usage:
RR =: 5 7 $ 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0
NB. First five gens of RR
life^:(i.5) RR
NB. in ASCII
' *' {~ life^:(i.5) RR
Explanation of code:
>,{ ;~(1 0 _1)
Produces all possible 1 rotations of a matrix y.
|. y
Applies 1 rotations to y
+/
Adds all 1 rotations leading to neighbor-count for each cell
=&
Creates a monadic function which takes a single number and compares it to the neighbor-counts
each 3 4
Applies the function to 3 and 4 to find cells with 3 or 4 surrounding cells including self alive.
>
Unboxes to apply bitwise and
(>(($ y) $ 1);y)
I want all cells that correspond to 3 (($ y) $ 1)
and only the cells that are already currently living the correspond to 4 (y)
.
+./
Or the result together to produce the next generation.
I would like to know if there's a better way to do this and shorten code. A lot of the things I did seem very hacky.