15
Life-like cellular automaton are cellular automaton that are similar to Conway's Game of Life, in that they operate on a (theoretically) infinitely large square grid, where each cell has exactly 8 neighbours, and is one of 2 states, namely alive and dead.
However, these Like-like versions are different in a crucial way: the rules for a given cell to come alive and the rules for a given cell to survive to the next generation.
For example, classic Game of Life uses the rule B3/S23
, meaning that it takes 3 alive cells to birth a new one, and either 2 or 3 living neighbours to survive. For this challenge, we will assume that neighbours do not include itself, so each cell has exactly 8 neighbours.
Your task is, given a starting configuration, a birth rule, a survival rule and a positive integer (the number of generations to be run), simulate the Life-like automaton using those rules for the number of generations given in the shortest code possible. The starting configuration will be a square matrix/2-dimensional array or a multiline string, you may choose. The others may be given in any reasonable format and method.
For example, if the birth rule was 12345678
(any living neighbours), the survival rule was 2357
and the starting configuration was
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
the next two generations would be
Generation 1: Generation 2:
0 0 0 0 0 1 1 1 1 1
0 1 1 1 0 1 1 0 1 1
0 1 0 1 0 1 0 1 0 1
0 1 1 1 0 1 1 0 1 1
0 0 0 0 0 1 1 1 1 1
If the number of generations given was 10, the output would be something along the lines of
0 1 1 1 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
0 1 1 1 0
You do not have to handle changes that happen outside of the bounds given by the input matrix, however, all cells outside the matrix begin dead. Therefore, the input matrix may be any size, up to the maximum value your language can support. You do not have to output the board between generations.
This is a code-golf so the shortest code wins.
Test cases
These use the B/S
notation to indicate the rules used
B2/S2
, generations = 100
, configuration:
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0
Output:
0 0 0 0 0 0 0 0
0 1 0 0 0 0 1 0
1 0 0 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 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
B1357/S2468
, generations = 12
, configuration:
1 0 1 0 1 0
0 1 1 0 1 0
1 0 0 0 0 0
0 0 0 0 0 1
1 1 1 1 1 0
0 1 1 0 0 1
Output:
0 1 0 0 0 0
0 1 1 1 1 0
0 1 0 1 1 0
1 1 1 0 0 0
0 0 1 1 1 0
0 1 1 0 0 0
If you need to generate more test cases, you can use this wonderful simulator. Please make sure to limit the board size
Is the simulation toroidal? – Erik the Outgolfer – 2017-10-20T15:35:52.827
@EriktheOutgolfer no, as the matrix is (theoretically) infinite in size – caird coinheringaahing – 2017-10-20T15:36:48.917
Also, can we assume the given matrix is square? – Erik the Outgolfer – 2017-10-20T15:46:27.510
2@EriktheOutgolfer "infinitely large square grid" – caird coinheringaahing – 2017-10-20T15:47:04.930
But it doesn't say you can assume that...will edit in. – Erik the Outgolfer – 2017-10-20T15:47:38.047
How will the input be given? Can we assume the starting configuration is given in the form of the 2D array/multiline string/etc? And can the birth and survival rules be given as lists/arrays of integers, or must we take them as a string as shown? – Arnold Palmer – 2017-10-20T16:26:35.147
@ArnoldPalmer you may take input in standard methods, "The starting configuration will be a square matrix/2-dimensional array or a multiline string, you may choose." and you can take them in any reasonable format – caird coinheringaahing – 2017-10-20T16:27:53.137