## 289. Game of Life

According to Wikipedia's article: "The **Game of Life**, also known simply as **Life**, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."

The board is made up of an `m x n`

grid of cells, where each cell has an initial state: **live** (represented by a `1`

) or **dead** (represented by a `0`

). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):

- Any live cell with fewer than two live neighbors dies as if caused by under-population.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by over-population.
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.

The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the `m x n`

grid `board`

, return *the next state*.

**Example 1:**

Input:board = [[0,1,0],[0,0,1],[1,1,1],[0,0,0]]Output:[[0,0,0],[1,0,1],[0,1,1],[0,1,0]]

**Example 2:**

Input:board = [[1,1],[1,0]]Output:[[1,1],[1,1]]

**Constraints:**

`m == board.length`

`n == board[i].length`

`1 <= m, n <= 25`

`board[i][j]`

is`0`

or`1`

.

**Follow up:**

- Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
- In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?

## Rust Solution

```
struct Solution;
#[derive(Copy, Clone)]
enum State {
Dead = 0,
Live = 1,
LiveToDead = 2,
DeadToLive = 3,
LiveToLive = 4,
DeadToDead = 5,
Unknown = 6,
}
impl State {
fn from_i32(x: i32) -> State {
match x {
0 => State::Dead,
1 => State::Live,
2 => State::LiveToDead,
3 => State::DeadToLive,
4 => State::LiveToLive,
5 => State::DeadToDead,
_ => State::Unknown,
}
}
fn to_i32(self) -> i32 {
self as i32
}
fn to_live(self) -> i32 {
match self {
State::LiveToDead | State::Live | State::LiveToLive => 1,
_ => 0,
}
}
fn next(self, neighbors: i32) -> State {
match self {
State::Live => match neighbors {
0 | 1 => State::LiveToDead,
2 | 3 => State::LiveToLive,
_ => State::LiveToDead,
},
State::Dead => match neighbors {
3 => State::DeadToLive,
_ => State::DeadToDead,
},
State::LiveToDead => State::Dead,
State::DeadToLive => State::Live,
State::LiveToLive => State::Live,
State::DeadToDead => State::Dead,
State::Unknown => State::Unknown,
}
}
}
impl Solution {
fn game_of_life(board: &mut Vec<Vec<i32>>) {
let n = board.len();
let m = board[0].len();
for i in 0..n {
for j in 0..m {
let mut neighbors = 0;
if i > 0 {
neighbors += State::from_i32(board[i - 1][j]).to_live();
}
if j > 0 {
neighbors += State::from_i32(board[i][j - 1]).to_live();
}
if i + 1 < n {
neighbors += State::from_i32(board[i + 1][j]).to_live();
}
if j + 1 < m {
neighbors += State::from_i32(board[i][j + 1]).to_live();
}
if i > 0 && j > 0 {
neighbors += State::from_i32(board[i - 1][j - 1]).to_live();
}
if i + 1 < n && j > 0 {
neighbors += State::from_i32(board[i + 1][j - 1]).to_live();
}
if i + 1 < n && j + 1 < m {
neighbors += State::from_i32(board[i + 1][j + 1]).to_live();
}
if i > 0 && j + 1 < m {
neighbors += State::from_i32(board[i - 1][j + 1]).to_live();
}
let current: State = State::from_i32(board[i][j]);
board[i][j] = current.next(neighbors).to_i32();
}
}
for i in 0..n {
for j in 0..m {
board[i][j] = State::from_i32(board[i][j]).next(0).to_i32()
}
}
}
}
#[test]
fn test() {
let mut board: Vec<Vec<i32>> = vec_vec_i32![[0, 1, 0], [0, 0, 1], [1, 1, 1], [0, 0, 0]];
let res: Vec<Vec<i32>> = vec_vec_i32![[0, 0, 0], [1, 0, 1], [0, 1, 1], [0, 1, 0]];
Solution::game_of_life(&mut board);
assert_eq!(board, res);
}
```

Having problems with this solution? Click here to submit an issue on github.