Conway's game of life

5

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

Muhammad Salman

Posted 2018-04-12T17:39:42.560

Reputation: 2 361

Question was closed 2018-04-12T19:40:14.580

Comments are not for extended discussion; this conversation has been moved to chat.

– Mego – 2018-04-12T22:40:18.777

Answers

1

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

Jonathan Allan

Posted 2018-04-12T17:39:42.560

Reputation: 67 804

Cool. Upvoted it. Although I have no clue how jelly works. – Muhammad Salman – 2018-04-12T19:28:03.800

I like the explanation. Thanks maybe I'll learn something. – Muhammad Salman – 2018-04-12T19:28:28.717

There is a good tutorial and wiki at the github page linked by "Jelly" in the header :) – Jonathan Allan – 2018-04-12T19:30:44.130

Cool. Thanks I will be sure to look into it. Thanks to everyone for helping me out. – Muhammad Salman – 2018-04-12T19:32:25.960