5
\$\begingroup\$

Description :

Given an array of strings representing a grid of cells

With the key :

0 = 'dead cell' 1 = 'live cell'

write a function that returns the next iteration following the rules of Conway's Game of Life .

Rules

  1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Examples :

["111" , "110" , "100"] //input
["101" , "001" , "110"] // output
/**
  * There will be other tests so no hardcoding.
**/

This is code golf shortest code wins. Good luck and have fun

\$\endgroup\$
1
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$
    – user45941
    Apr 12, 2018 at 22:40

1 Answer 1

1
\$\begingroup\$

Jelly,  40  38 bytes

z0j@€0,0µ⁺ṡ3Z€ṡ€3S€€⁺Ḥ
L€ḣ@"Ç+e€€7,9,6

A monadic link accepting and returning lists of lists of ones and zeros

Try it online!

How?

Cells will be alive in the next generation if:

  1. it was alive in a neighbourhood (including itself) of three or four

  2. it was dead in a neighbourhood of exactly three

Therefore if double the neighbourhood plus "was alive" is seven or nine (case 1) or six (case 2) then it will be alive next generation.

(For all other possible cases the sum of double the neighbourhood and "was alive" are other integers in the range [0,19])

z0j@€0,0µ⁺ṡ3Z€ṡ€3S€€⁺Ḥ - Link 1, neighbourhood counts * 2: board
        µ⁺             - perform this monadic link twice:
z0                     -   transpose with filler zero
     0,0               -   list [0,0]
  j@€                  -   join €ach with swapped arguments (surround each with zeros)
          ṡ3           - all overlapping sub-lists of length three (i.e. lists of rows)
            Z€         - transpose €ach
              ṡ€3      - all overlapping sub-lists of €ach of length three (i.e. 3x3 neighbourhoods)
                 S€€⁺  - repeated sum for €ach for €ach (neighbourhood sum)
                     Ḥ - double
                       - Note: the result includes counts of off-board areas filling right

L€ḣ@"Ç+e€€7,9,6 - Main link: board
     Ç          - call the last link (1) as a monad (get 2 * neighbourhood counts)
L€              - length of each row of the input board
  ḣ@"           - zip with head to index with swapped arguments
                - (i.e trim the neighbourhood counts to the shape of the input board)
      +         - add (vectorises) (i.e. 2 * neighbourhood count + was alive)
          7,9,6 - list = [7,9,6]
       e€€      - exists in? for €ach for €ach
\$\endgroup\$
4
  • \$\begingroup\$ Cool. Upvoted it. Although I have no clue how jelly works. \$\endgroup\$ Apr 12, 2018 at 19:28
  • \$\begingroup\$ I like the explanation. Thanks maybe I'll learn something. \$\endgroup\$ Apr 12, 2018 at 19:28
  • \$\begingroup\$ There is a good tutorial and wiki at the github page linked by "Jelly" in the header :) \$\endgroup\$ Apr 12, 2018 at 19:30
  • \$\begingroup\$ Cool. Thanks I will be sure to look into it. Thanks to everyone for helping me out. \$\endgroup\$ Apr 12, 2018 at 19:32

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