14
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ Welcome to Code Review! Thanks for including the explanations with your code. \$\endgroup\$ – 200_success Apr 4 '16 at 21:18
1
\$\begingroup\$

I have written APL code on VM/CMS and in DEC's VAX-11 interpreter.

No, I don't see a better way that would shorten the code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.