Langton's Ant
Turmite cellular automaton. Ant moves on grid: white→turn right, black→turn left. Creates emergent highway after ~10k steps. Simple rules, complex behavior.
Visualization
Interactive visualization for Langton's Ant
Langton's Ant:
- • Cellular automaton
- • White→turn right, Black→turn left
Interactive visualization with step-by-step execution
Implementation
1class LangtonsAnt {
2 private grid: Map<string, boolean>;
3 private x: number = 0;
4 private y: number = 0;
5 private dir: number = 0; // 0=N, 1=E, 2=S, 3=W
6
7 step(): void {
8 const key = `${this.x},${this.y}`;
9 const isBlack = this.grid.get(key) || false;
10
11 if (isBlack) {
12 this.dir = (this.dir + 3) % 4; // Turn left
13 this.grid.set(key, false); // Flip to white
14 } else {
15 this.dir = (this.dir + 1) % 4; // Turn right
16 this.grid.set(key, true); // Flip to black
17 }
18
19 // Move forward
20 if (this.dir === 0) this.y--;
21 else if (this.dir === 1) this.x++;
22 else if (this.dir === 2) this.y++;
23 else this.x--;
24 }
25
26 simulate(steps: number): void {
27 for (let i = 0; i < steps; i++) {
28 this.step();
29 }
30 }
31}Deep Dive
Theoretical Foundation
Rules: At white square: turn 90° right, flip color, move forward. At black square: turn 90° left, flip color, move forward. Behavior: chaos (0-500 steps), order (500-10k), highway (10k+). Unpredictable from rules alone.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
Studying emergence and self-organization
Complexity theory research
Random walk and diffusion modeling
Pattern generation in computer graphics
Educational demonstrations of simple rules
Artificial life and swarm behavior studies
Use Cases
Related Algorithms
A* Search Algorithm
Informed search algorithm combining best-first search with Dijkstra's algorithm using heuristics. Widely used in pathfinding and graph traversal, A* is optimal and complete when using admissible heuristic. Used in games, GPS navigation, and robotics. Invented by Peter Hart, Nils Nilsson, and Bertram Raphael in 1968.
Convex Hull (Graham Scan)
Find smallest convex polygon containing all points. Graham Scan invented by Ronald Graham in 1972, runs in O(n log n). Essential in computational geometry, computer graphics, and pattern recognition.
Line Segment Intersection
Determine if two line segments intersect. Fundamental geometric primitive used in graphics, CAD, GIS. Uses orientation and collinearity tests.
Caesar Cipher
The Caesar Cipher is one of the oldest and simplest encryption techniques, named after Julius Caesar who used it to protect military messages around 100 BC. It works by shifting each letter in the plaintext by a fixed number of positions down the alphabet. For example, with a shift of 3, A becomes D, B becomes E, and so on. Despite being used for over 2000 years, it's extremely weak by modern standards with only 25 possible keys, making it trivially breakable by brute force or frequency analysis.