THE ANT
Well, sorry to disappoint you but this ant is not a species of Ant family,
neither is it someone's pet.
The concept of Langton's Ant started popping up around 1986 when Chris
Langton was high(/j) and thought of some really high ant with no definite
purpose but followed some specific set of rules.
The ant is first placed at a grid where each square can change colors to
either white or black. The ant can move into any of the four cardinal
direction at each step it takes.
The rules for the ant were:
At a white square, turn 90° clockwise, flip the color of the square, move
forward one unit
At a black square, turn 90
counter-clockwise, flip the color of the square, move forward one
unit.
Well seems pretty simple, what's the kick to it.
In the first 10,000 steps(yes, the ant never stops, it is very hardworking,
unlike you!)
the ant follows a really random and chaotic path wondering about it's
existence while following the rules that were given above.
But something interesting happens after the 10,000 steps, the ant figures
that it is inside a simulation and tries to get out of it by following a
recurring pattern of 104 steps that the crazy mathematicians refer as the
"highway", well personally I like to refer to it as "the giant boner".
You get why I referred to it as a boner right, RIGHT?
THE INSPIRATION
Anyway, let me tell you how I came across this ant.
I watch a lot of Numberphile
videos(yes, I am a Numberphile addict), and I do this thing where I watch
a lot of Numberphile videos in one stretch before I sleep. Like I play a
specific video and let it auto-play till I feel sleepy or realize that I
have a life.
During one of these sessions, I came across The Langton's Ant video. This video came out around 7 years ago. I'm pretty sure, the
craze for this Ant might've took off after this video.
It is a beautiful video, you should watch it really. They explain the ant
in an interesting way. Well to be honest, most of the blogs in this will
have it's inspiration from one of Numberphile videos.
After watching this video, my mind convinced me that I NEED to code this
ant in whatever way possible. Well that is what made this blog possible
anyway.
So I headed out, coding this crazy ant.
THE PROCESS
So when I started thinking of the process, my mind instantly thought of the
python package turtle. Turtle is a really straightforward approach and I
could've completed this ant in like minutes of coding.
But I hate python for some reasons that will be discussed in future blogs.
I personally feel like python should only belong to data science, or that is
how I use it.
So I scratched the turtle idea.
My mind then immediately jumped to the snake game. By seeing the grids, my
mind thought this was really similar to the snake game. And luckily I was
already building a snake game for my practice using the basic HTML, CSS
& JS. You can find the code for this here.
I then decided to make the Langton's Ant using the same.
A lot of inspiration to do this code also came from my dear friend Nox, who
motivates me so much to do stuff. Thank you Nox :)
Anyway, coming back to the snake game.
- The snake game uses a grid system, but I decided to make use of the canvas function in JS for the Ant.
- The movements were automated for the ant instead of the manual movements for the snake.
- The snake had variable length while our ant has a fixed length of 1 block.
- I altered the food, in the snake game to be the black and white squares.
Well you might ask, "Suraj, why these extra steps? "
My only answer to this is "I just feel like doing it."
Anyway, after a few milliseconds of coding(not really, it took an
embarrassingly long time to code this because I was getting distracted by
youtube and gaming sessions), I finally completed the code.
Since this code doesn't necessarily have any beautificationy material, i
tried my best to make it look good.
THE OUTPUT
Here it is, the final output of my teeeny weeny code. We will divide it
into three parts, part 1 will have 15,000 moves, part 2 will have 100,000
moves and part 3 will have 2,000,000 moves.
Let's Go
15000 MOVES
These moves are the ones you see around the internet everywhere and anytime
you search the term "Langton's Ant".
But I hope my point of the "highway" being a boner is a bit clear
now.
Anyway, as you can see in the video, you can see the ant moving like crazy
and being chaotic for the first 10k moves and then going off to a distant
highway, trying to escape the simulation it is trapped in.
100000 MOVES
In 100000 moves, I make the edges of my canvas as a wormhole so that the ant can again come through to the opposite side of the screen so that it stays trapped in the simulation.
The pattern it makes, looks similar iron cables slowly starting to rust.
When the ant meets the chaos it had already been created, it gets a bit more crazy but the ant makes it way to build another highway of escape, only to come back into the chaos.
1000000 MOVES
For this version, I sped up my ant almost 1000 times to induce more chaos and destruction. But turns out, as the moves got increased, I couldn't take my eyes off the screen, it felt like I was getting trapped in the simulation, the ant was really pulling me in.
The pattern this one makes is like the advanced version of the 100k one.
It dwells deep into spreading more rust to the added iron cables that the highways make.
It's really interesting how simple algorithm and math can do, this is what inspired me into making these blogs in the future. I decided that I will code something similar every week and try to blog it here.
This is really exciting for me and I hope you had a fun time reading this and I hope you have a nice day!!!
BUBBYEEE
looks so fun!
ReplyDeleteso cool!!
ReplyDelete