Life is Simple
The game of Life by John Conway is played on a grid.
At start we place several "organisms" into some cells
(only one organism per cell), leaving other cells empty. Cells which are touching by side or corner are neighboring,
so each organism may have from
After this, at each turn the colony of organisms evolve by the following rules:
2or more than
3neighbors will die (not passing to next turn);
3neighbor organisms, a new one is born.
It is important, that birth and dying are performed simultaneously. So when programming you need work like this:
Instead of marking cells you can store their coordinates in two dedicated arrays or lists to process them later.
Let us see an example:
- - - - - - - - - - - - - - - - - - - - - - X - - - - - - - - X X X - => - - X - - => - X X X - - - - - - - - X - - - - - - - - - - - - - - - - - - - - - -
Here is a colony of
3 organisms. Obviously, at the first turn two of them (left and right) should die. However,
before dying they will participate in giving birth to another two ones. Really, empty cells just above the central and
just below it have exactly
3 neighbors. So after first turn the colony looks the same but rotated by 90°.
Of course after second move the configuration will return to exactly such form as it was initially.
In this problem you will run the given configuration for 5 turns and print the number of organisms after each step.
Input data will contain
5 lines of
7 characters each. They represent a
5 by 7 fragment of the game field.
"-" represent empty cells, while each
"X" represent a cell containing a live organism.
Answer should contain
5 values separated by spaces - the amounts of live organisms after each turn.
input data: ------- ---XX-- -XXX--- ------- ------- answer: 6 5 3 2 0
Note: if you will implement the game using a kind of
2D array, make sure it is large enough and the initial
configuration is placed far from edges, so that it will not reach them in
5 moves - otherwise your results could
You also may be interested in solving an advanced version of the problem Hard Life which may require to come up with more efficient algorithm.