Stewie's Game of Life and Fatigue is quite similar to the more famous Conway's Game of Life.
The universe of the Stewie's Game of Life and Fatigue (GoLF) is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of three possible states, alive, dead or tired. Every cell interacts with its eight neighbors, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
- Any cell that has been alive for two consecutive generations dies, as if by fatigue. It can't wake to life again until next generation
- Any cell that is outside the boundary of the input grid are dead, as if it's fallen off a cliff.
Challenge:
Your challenge is to take a grid of dimensions n-by-m representing the initial state of a GoLF, and an integer p, and output the state of the Game after p generations.
Rules:
- Input and output formats are optional, but the input/output grids should have the same representation
- You may choose any printable symbols to represent live and dead cells (I'll use
1
for live cells and0
for dead cells). - You may choose if you have 0 or 1-indexed. In the examples,
p=1
means the state after one step. - Shortest code in each language wins
- Built-in function for cellular automation are allowed
Test cases:
In the examples, I've only included the input grid in the input, not p. I've provided outputs for various p-values. You shall only output the grid that goes with a given input p.
Input:
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0
--- Output ---
p = 1
0 0 0 0 0
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0
p = 2
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
p = 3 -> All dead
---
Input:
0 1 0 0 0 0
0 0 1 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
--- Output ---
p = 1
0 0 0 0 0 0
1 0 1 0 0 0
0 1 1 0 0 0
0 1 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
p = 2
0 0 0 0 0 0
0 0 0 0 0 0
1 0 0 0 0 0
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