Imagine you have a sheet of grid paper with the left and right sides joined together to make a cylinder. Each square on the grid is either a period . or a star *.
Starting with some pattern in the top row of the grid, we determine the next row by applying a simple rule:
- for each position in the grid:
- put a star in the grid if there is a star either to the left or the right in the row above, but not both.
- otherwise put a period.
There are eight different possible configurations of the left, middle and right cell in the top row. The eight patterns are shown below with the corresponding middle cell in the new row. We have put vertical lines in for clarity, your program should
not put these in:
|*|*|*| |*|*|.| |*|.|*| |*|.|.| |.| |*| |.| |*| |.|*|*| |.|*|.| |.|.|*| |.|.|.| |*| |.| |*| |.|
Again, just to be clear, we only print a * in the middle cell in the row below when there is a * in either the left or the right cell in the row above, but not both.
This is a 1-dimensional cellular automaton (http://en.wikipedia.org/wiki/Cellular_automaton). Cellular automata are very simple computational systems inspired by biology. The state of each cell is either on (a star in our example) or off (a period), and the state of each cell in the future is governed by simple rules about the current state of the cell and its neighbours. In our 1-dimensional automata, the
future is the next row of output.
Your program will simulate the cellular automata described above. It should read a number and a string from raw_input. The number will indicate the number of rows to generate, and the string is the starting row. It should then print out the first row and all subsequent rows until the number of rows printed is the number of rows that were requested (the number from raw_input).
For example:
Number of lines: 8
Start line: ........*.........
........*.........
.......*.*........
......*...*.......
.....*.*.*.*......
....*.......*.....
...*.*.....*.*....
..*...*...*...*...
.*.*.*.*.*.*.*.*.. This type of machine is called a
cellular automata because it is inspired by the way bacterial cells grow. Perhaps the most famous of these is the two dimensional version called Conway's Game of Life (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life).