How to optimally solve and generalize this Conway's Game of Life puzzle
My "Game of Life" implementation:
a = RandomInteger[BernoulliDistribution[0.1], {200, 200}];
Dynamic[Image[a =
1 - Unitize[(# - 3) (# - 12) (# - 13)] &@
ListConvolve[{{1, 1, 1}, {1, 10, 1}, {1, 1, 1}}, a, {2, 2}, 0]
]]
To use the initial grid in the question you would just initialise a
with:
a = SparseArray[list -> 1]
However my code continuously updates the grid (quite rapidly), so the grid evolves to the "all dead" state in a fraction of a second.
To actually answer the question "how many generations have any life in them" you could write the evolution step as a function and use FixedPointList
to count the number of steps:
evolve[a_] := 1 - Unitize[(# - 3) (# - 12) (# - 13)] &@
ListConvolve[{{1, 1, 1}, {1, 10, 1}, {1, 1, 1}}, a, {2, 2}, 0]
Length@FixedPointList[evolve, Normal[SparseArray[list -> 1]]] - 2
(* 3 *)
Note that FixedPointList
will generate the complete list of grids, so for larger problems you would want to use a different approach to avoid eating all your computer's memory.
This is one way of implementing Conway's Game of Life:
step[matrix_] := Module[{m = matrix, neighbours},
neighbours = ListConvolve[{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}}, ArrayPad[m, 1]];
m = ReplacePart[m, Position[neighbours, _?(# == 1 || # > 3 &)] -> 0];
m = ReplacePart[m, Position[neighbours, 3] -> 1]
]
For example, this is the so called glider:
m = ArrayPad[{{0, 1, 0}, {0, 0, 1}, {1, 1, 1}}, {1, 10}];
ListAnimate[ArrayPlot[#, Mesh -> True] & /@ NestList[step, m, 30]]
I've written about another cellular automata problem here and this includes a link to a post where image processing was used in Mathematica to get initial states for a cellular automata problem, so the answer to that question is that yes it is possible.