This question already has an answer here:

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 .


  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

marked as duplicate by Peter Taylor code-golf Apr 12 at 19:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Comments are not for extended discussion; this conversation has been moved to chat. – Mego Apr 12 at 22:40

Jelly,  40  38 bytes


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

Try it online!


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
  • Cool. Upvoted it. Although I have no clue how jelly works. – Muhammad Salman Apr 12 at 19:28
  • I like the explanation. Thanks maybe I'll learn something. – Muhammad Salman Apr 12 at 19:28
  • There is a good tutorial and wiki at the github page linked by "Jelly" in the header :) – Jonathan Allan Apr 12 at 19:30
  • Cool. Thanks I will be sure to look into it. Thanks to everyone for helping me out. – Muhammad Salman Apr 12 at 19:32

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