Langton’s Ant is a 4-state two-dimensional universal Turing machine. It was invented by Chris Langton in 1986. It is basically an ant, sitting on a square lattice of cells, which are initially white. The ant moves on the plane and changes the color of cells creating patterns on it. But the movement of the ant is not random; it follows the following set of rules :
- If the ant is on a black square, it turns right 90 degrees and moves forward one unit.
- If the ant is on a white square, it turns left 90 degrees and moves forward one unit.
- When the ant leaves a square, it inverts the color.
As the ant starts, it creates a black and white pattern while moving. Initially, the changes are not distinctive but as we iterate it over and over again, a beautiful pattern emerges. But if we further increase the number of iterations (say ~ 10000), the ant starts repeating its path with a gradual shift, instead of making new patterns. Thus, we obtain a highway like pattern that is infinite. The ant keeps moving on that highway and gives the following pattern.
Refer the visual explanation of Langton’s Ant from here. It helps to visualize how exactly the ant works.
The Python-3 code for Langton’s Ant is given below :
Here, the white cells are denoted by ‘ ‘ (space) and the black cells are denoted by ‘•’ (dot).
- Important differences between Python 2.x and Python 3.x with examples
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- Python | Sort Python Dictionaries by Key or Value
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to a Python Script
- Python | Add Logging to Python Libraries
- Python | a += b is not always a = a + b
- set add() in python
- Any & All in Python
- SHA in Python
- chr() in Python
- Use of min() and max() in Python
- SQL using Python | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.