Langton’s ant is a two dimensional, 4 state universal turing machine with simple set of rules. In 1986, Chris Langton invented it. It is an ant that is sitting on a square lattice of cells. These cells are initially white, but as the ant moves, it changes color, which creates a pattern. But we should remember that the ant’s movement is not random, but it follows a certain set of rules. Now, we will write the code for it in python for data science.
- In case the ant is on a black square, it will turn right 90 degrees and move one unit in the forward direction.
- In case the ant is on a white square, it will turn left 90 degrees and move one unit forward.
- As soon as the ant leaves a square, it will invert the color.
As soon as the ant starts moving, it will create a black and white pattern. In the beginning, the change will not be distinctive, but as we keep on iterating, we will observe a beautiful pattern. But as number of iterations increases, the ant will repeat its path, and it will shift gradually and not make a new pattern. We will obtain a highway like pattern which is infinite. The ant will give the following pattern.
In Python 3, we will generate the Langton’s ant by using the following code.
In the above example, we have denoted white cells by ‘ ‘ and black cells by ‘.’