r/dailyprogrammer • u/Elite6809 1 1 • Jul 30 '14
[7/30/2014] Challenge #173 [Intermediate] Advanced Langton's Ant
(Intermediate): Advanced Langton's Ant
If you've done any work or research onto cellular automata, you may have heard of Langton's Ant. It starts with a grid similar to that of Conway's Game of Life where a grid cell can be black or white, however this time we have an 'ant' on it. This little metaphorical ant will follow these four rules at every 'step':
- If the current square is white, turn the ant 90' clockwise
- If the current square is black, turn the ant 90' anticlockwise
- Flip the colour of the current square
- Move forward (from the ant's perspective) one cell
With the following starting conditions:
- All cells start white
- The ant starts pointing north
However, being /r/DailyProgrammer, we don't do things the easy way. Why only have 2 colours, black or white? Why not as many colours as you want, where you choose whether ant turns left or right at each colour? Today's challenge is to create an emulator for such a modifiable ant.
If you have more than 2 colours, of course, there is no way to just 'flip' the colour. Whenever the ant lands on a square, it is to change the colour of the current square to the next possible colour, going back to the first one at the end - eg. red, green, blue, red, green, blue, etc. In these cases, at the start of the simulation, all of the cells will start with the first colour/character.
Input Description
You will be given one line of text consisting of the characters 'L' and 'R', such as:
LRLRRThis means that there are 5 possible colours (or characters, if you're drawing the grid ASCII style - choose the colours or characters yourself!) for this ant.
In this case, I could choose 5 colours to correspond to the LRLRR:
- White, turn left (anticlockwise)
- Black, turn right (clockwise)
- Red, turn left (anticlockwise)
- Green, turn right (clockwise)
- Blue, turn right (clockwise)
You could also choose characters, eg. ' ', '#', '%', '*', '@' instead of colours if you're ASCII-ing the grid. You will then be given another line of text with a number N on it - this is the number of 'steps' to simulate.
Output Description
You have some flexibility here. The bare minimum would be to output the current grid ASCII style. You could also draw the grid to an image file, in which case you would have to choose colours rather than ASCII characters. I know there are some people who do these sorts of challenges with C/C++ curses or even more complex systems.
14
u/Elite6809 1 1 Jul 30 '14 edited Aug 29 '14
I wrote this solution in C#. It uses a custom data type ExtendableGrid<T> that can dynamically extend any of its borders. This saves to an image file. https://gist.github.com/Quackmatic/f9b0d9fc4b6f739d922d (currently only supports up to 10 colours - to allow more, add more of your own colours to the
colorsarray.)Here's an example with state
LRRRRRLLR.After 100000 ticks: http://i.imgur.com/pgOAiXF.png (scaled up 4x)
250000: http://i.imgur.com/VYB5rUd.png (scaled up 3x)
1000000: http://i.imgur.com/gKbH0QS.png
3000000: http://i.imgur.com/OjqYWJa.png
11
u/skeeto -9 8 Jul 30 '14 edited Jul 30 '14
C. It supports up to 9 colors and the simulation is run as a state machine using circular linked lists. It outputs a PPM image (Netpbm). The state machine is a fixed size (
SIZE), allowing me to avoid dynamic allocation.Here's the output of
LLLLRRLLR 5000000. What's cool about this one is that it's tileable.And here's
LLRR 10000000which is my favorite so far:9
u/chunes 1 2 Jul 31 '14 edited Jul 31 '14
Java (tried some 8 features!):
https://gist.github.com/anonymous/57094a7127c3810d8bef
Pretty pictures!
And a big one:
EDIT:
This image shows that the smoothing effect only happens near the edges:
Yup, it's Langton's Ant:
Changing the hue along with the brightness while selecting colors:
7
u/[deleted] Aug 03 '14
Okay so I might've gone a bit overboard with this one. My solution is done in C# and has the following features:
The GUI is limited to a 600x600 image, but images generated from the command line tool can be any size.
Here's a screenshot:
http://i.imgur.com/SlZuJS0.png
And the (messy) source on github:
https://github.com/mfatica/Langtons-Ants
5
u/hutsboR 3 0 Jul 30 '14 edited Jul 30 '14
Dart: Opted for the ASCII output for now. I was going to come up with a web solution with some sort of grid but I'm not familiar enough with the language.
Commented, slightly cleaner version!
Usage:
PASTE: LLRRL 3000 TICKS, RRLL 1000 TICKS, 20X20
IMAGE: RRLLLRLLLRRR, 30K TICKS, 80X80 (SCALED)
IMAGE: LRRRRRLLR, 150K TICKS, 240X240, CONSOLE IMAGE
4
u/[deleted] Jul 30 '14
Fun Langton's Ant (2 color) tidbit:
I think it's still an open problem if, given any grid coloring of any finite rectangle R, a Langton's Ant inside R will always eventually make its way outside of R.
1
u/emsimot Aug 01 '14
My first challenge! Mine is written in javascript using a <canvas> element.
Here is the result of 20,000 iterations of LLRR. I think there might be something subtly wrong as it is not perfectly symmetrical.
I'm still new to javascript so please feel free to criticize!
1
u/PalestraRattus Aug 03 '14 edited Aug 03 '14
C# Updated source. It uses a 30x30 table element. Rather than expand the borders as needed. It's treated like a sphere. Where X Max loops to X Min, and Y Max loops to Y Min, and so on. I like Elite's grid element a lot better. But the table was the first that came to mind for me so I just ran with it. *I know the code is bloated, and the variables could be simplified with arrays. When in a hurry I tend to build, and then compress/clean my code. The code on codeplex is not "clean" in any sense of the word.
Source + Example Images
https://dpc173.codeplex.com/
Livestream of the program running
http://vaughnlive.tv/Warcarrier
1
u/theCodest Aug 08 '14
LLRR. 1.000.000 ticks http://s22.postimg.org/wtuw8gogx/LLRR.jpg Used C#, win form.
Please, Please, Please take a look at my code and criticize the crap out of it! I need it! Thanx!! https://github.com/valiman/Langtons-Ant
3
u/Frichjaskla Jul 30 '14 edited Jul 30 '14
C++11 using SDL2
http://imgur.com/NVeZiao http://imgur.com/2ZMUiCy
Takes ant rule as the parameter: ./langton LLRR
up/down arrow to increase/decrease gpf, generations pr frame
stores a table with states and uses a look up table to turn that into colors, which in turn are used for a texture.
3
u/[deleted] Jul 31 '14
This is one of the coolest problems so far, especially with all the images submitted.
4
u/MaximaxII Jul 30 '14 edited Jul 30 '14
I thought that this one would be really hard, but honestly, the hardest thing was installing Pillow - here's some advice for everyone else doing this challenge: keep your head cool, break it into smaller challenges, don't be scared to use many variables to describe the ant's progress...
So here we go :) Feedback and criticism are welcome, as always.
Challenge #173 Intermediate - Python 3.4
Output
I tried to replicate Wikipedia's example, and got this.
2
u/[deleted] Jul 30 '14 edited Jan 02 '16
2
u/MIXXEDDOWN Jul 31 '14 edited Aug 01 '14
Python animation with turtle library
Here 1000 iterations for LLRR.
EDIT -- More images
LRLLLRLRLRRLRRLRRRLRLRLRLRLRRRLRLRLRLRRLLRRLRLRLRLRLLLLRRRRRRRLRLRLRLRLRLRLRRRLRLRLRRRLLLLLRRLRRLRLRLLLRLRLRRL
LRRRRRLLR
2
u/13467 1 1 Jul 31 '14
In Elm, a function reactive language that compiles to HTML and javascript. Try the result here.
2
u/minikomi Aug 01 '14
My solution in racket in a gist:
https://gist.github.com/minikomi/28c1d077845d6e48fadb
And here's an example of the output:
http://www.gfycat.com/SnoopyCourageousFunnelweaverspider
Run it in dr racket and then in the REPL type:
eg
is on a size 100 x 100 px board, scaled double, with 1000 iterations per frame.
3
u/MrP_123 Jul 30 '14 edited Jul 31 '14
My solution in Java. It still has some bugs that I can't find (OutOfBounds), because my grid is a fixed size. Otherwise it works great. I hope you guys can give me some feedback on my code.
It supports 9 colors and saves the finished image to your desktop.
https://gist.github.com/MrP123/0dd6c04bbf62a01d2e61
The output on a 1000 x 1000 white grid:
http://imgur.com/Y7rxmpD
2
u/dohaqatar7 1 1 Jul 30 '14 edited Jul 30 '14
This is my solution: Java
My solution allows for three types of grids
RigidGrid,ToroidalGridandExpandingGrid. Each acts in a slightly different way, mainly how it reacts when the "ant" goes out of bounds. In addition, I also allowed for diferent amounts of turning (0-360), and multiple "ants" present at a time.5,000,000 iterations on LRRRRRLLR
2
u/jeaton Jul 30 '14
JavaScript and HTML Canvas
Demo
Code:
2
u/[deleted] Jul 30 '14
[M]y first time so please be gentle!
My entry is in Java
https://github.com/hughwphamill/langtons-ant
Characteristics
Here's a test using /u/Elite6809's input characteristics of
LRRRRRLLR : 1000000
http://i.imgur.com/oTsqSAR.png
LRRRRRLLR : 3000000
http://i.imgur.com/Aj9gahP.png
It was fun, thanks for posting such interesting challenges!
1
u/viciu88 Jul 30 '14 edited Jul 30 '14
Java 1.8
Fields held in simple HashMap (limited only by memory size and int coordinates), random colors created if necessary.
LRRRRRLLR after 5000000 steps: http://i.imgur.com/7nWYp1d.png
Source: http://pastebay.net/1472917
There's also gif creation support but needs predefined coordinates to work properly. (and only 2048 frames, but still works tho).
1
u/Godspiral 3 3 Jul 30 '14 edited Jul 30 '14
in J,
utilities for meta programming:
made so that directions could be expanded to include diagonals
DIRS =: 4 2 $ 0 _1 1 0 0 1 _1 0
am =: 4 : '(0{:: x) (1{ x)} y'
viewmat gives nicer pictures for larger iterations
I get the blobby versions of other people rather than the splashy lines versions.
1
u/marchelzo Jul 31 '14
Haskell using JuicyPixels to ouput PNGs:
It can do anywhere from 1 color to 10 colors depending on the length of the input.
I used some of the cool looking sample inputs that I saw in the thread:
http://i.imgur.com/ko2vVO8.png (skeeto)
http://i.imgur.com/17SzZnx.png (dohaqatar7)
Performance wise I'm not sure how it stacks up against some of the other solutions, but it can do ~10,000,000 iterations in about 10 seconds as long as the ant doesn't start running off in a straight line.
I would appreciate any feedback :)
1
u/flugamababoo Jul 31 '14
I made a version in Python 3 that supports up to 224 colors and outputs a .bmp file containing the image. The bitmap is generated without need of any additional libraries. Half the code is for generating the bitmap, the other half runs the simulation.
If the ant wanders off the edge of the image, it wraps to the opposite side. It starts in the center of the bitmap. The color selection algorithm is not great (it doesn't produce extremely visually distinctive colors) but it works.
Usage: py ant.py 250000 LRRRRRLLR output.bmp 250 250
This runs 250000 cycles using turns defined by LRRRRRLLR, saved to output.bmp, with a resolution of 250 x 250 pixels.
output: http://imgur.com/a/gGfXu
1
u/mathcm Jul 31 '14
I still didn't fully test it because I'm sleepy right now. You can add up to 10 commands/colors.
But here it goes, on C (Please give me feedback):
include <stdio.h>
1
u/cooper6581 Jul 31 '14
Super fun challenge. Thanks!
Python using pillow
Output of ./intermediate.py 2000000 LLRRRRLL:
http://imgur.com/l1hGCAf
1
u/[deleted] Jul 31 '14
Written in Java. Using JFrame to show the data.
http://imgur.com/r9v8jc7
http://imgur.com/cdsbR3f
startBoard Class
Board Class
Ant Class
1
u/Octopuscabbage Aug 01 '14
Not the prettiest haskell solution
I also cheated a tiny bit using the matrix.
1
u/dp_account Aug 02 '14
Python 3.4 It exports to the PGM image format (Netpbm).
Output as pgm or as png (viewable in browser) for LRRRRRLLR after 5,000,000 steps.
1
u/YuriKahn Aug 02 '14
Sorry for the slightly late response, but this is my first daily programming attempt.
Done in Java.
Example input taken from previous poster: LRLLLRLRLRRLRRLRRRLRLRLRLRLRRRLRLRLRLRRLLRRLRLRLRLRLLLLRRRRRRRLRLRLRLRLRLRLRRRLRLRLRRRLLLLLRRLRRLRLRLLLRLRLRRL 4000000
Image: http://i.imgur.com/hnjnW46.png
Source: http://pastebin.com/nJ5Bmfir
1
u/nothingtofollow Aug 02 '14
Solution in Python using PIL library. Colors are chosen randomly.
Example with state 'LRRRRRLLR' after 1kk iterations: http://i.imgur.com/5A6OvDF.png
1
u/Regimardyl Aug 02 '14 edited Aug 02 '14
My Haskell solution. I was heavily inspired by /u/marchelzo's solution here.
Some Notes:
Example output after 20 000 000 iterations of LRRRRRLLR: http://i.imgur.com/X8fd8MM.png
1
u/PalestraRattus Aug 03 '14 edited Aug 03 '14
C# New solution to my previous one, I would love feedback. Instead of a table I'm now using an image. The image on load expands to your primary monitors working space. The image treats each pixel as a block. The ant starts at a random location anywhere on the "map", and otherwise behaves the same. It will also use a torus wrap-around mechanic *thanks to Elite for teaching me this term. By this if the ant would move too far west it loops to the farthest east. If it would move too far north it loops to the farthest south, and so on.
Output example after 10,000 turns...Order from Chaos!!! http://i.imgur.com/nLGJlu0.png
Live stream of this in action can be viewed at http://vaughnlive.tv/Warcarrier
I'll be streaming this within a couple hours of posting. I'll update this post with a link when it's live.
1
u/DrWhatsisname Aug 03 '14 edited Aug 03 '14
Did this in Python using a defaultdict for the grid and HSL for color so that I could just choose evenly spaced hues for each color: https://gist.github.com/DrWhatsisname/7183c414942cf7f10c93.
Some examples:
LRLRLL at 5 million iterations
LRLRLL at 10 million iterations
I tried the fourth iteration of the dragon curve, and interestingly it ended up as just a straight line. Here it is at 10 million iterations.
It starts looking pretty cool with more colors, since you get a cool rainbow effect around the edges of big shapes. LRLRLLRLRLRLRLLRLRLLRRRLRLLRLRLRLRLLRLRLRLRLRLLRLLRLLLRLRLLRRRRLRLLLR at 5 million iterations LRLRLLRLRLRLRLLRLRLLRRRLRLLRLRLRLRLLRLRLRLRLRLLRLLRLLLRLRLLRRRRLRLLLR at 10 million iterations
Those last two may be switched, the file names are too long to read.
1
u/Reverse_Skydiver 1 0 Aug 03 '14
Really, really late to this. Here's my solution in Java. Just a couple of examples:
500x500px, 10,000,000 ticks. RLLR.
600x600px, 100,000,000 ticks. LLRR.
1
u/jpverkamp Aug 09 '14
Bit late to the party, but here's a nice quick Racket solution (using complex numbers for indexing makes things fun):
The rendering function is a bit longer, but still not at all that bad:
The rest of the writeup (including a graphical view instead) is on my blog: Langton's Ant @ jverkamp.com
1
u/MechaTuring Aug 10 '14
Rust implementation
output to the terminal, might update to interface with image library.
didn't end up as clean as I'd like it to be (in particular the direction/rotation addition override, and the Plane.board containing indexes into the tile array rather than a pointer to the tile itself (was fighting with rust over the reference lifetimes, Rust won :/))
additionally, I think my x/y are flipped :/
1
u/shake_wit_dem_fries Aug 12 '14
Solution in Go. It's all set up to write out a gif, but I couldn't get Go's image library to cooperate with writing single gif frames out.
1
u/balda132 Aug 15 '14 edited Aug 15 '14
Probably too late but i found this challenge yesterday and tought it was really interesting. I wrote my solution in Java. I'm assuming a 5000 x 5000 image, background color is black and the other colors are generated randomly. I also added the possibility of using more than 1 ant.
Grid:
Ant:
Direction:
Up:
Down:
Left:
Right:
And some results:
LRRRRRLLR - 10.000.000 iterations, 1 ant
LRRRRRLLR - 100.000.000 iterations, 1 ant
LRRRLLLRLLLRRR - 1.000.000.000 iterations, 1 ant
LRRRRRLLR - 5.000.000 iterations, 3 ants
LRRRRRLLR - 10.000.000 iterations, 4 ants
RLLR - 100.000.000 iterations, 1 ant
RLLR - 1.000.000.000 iterations, 1 ant
RLLR - 5.000.000 iterations, 3 ants
1
u/ENoether Jul 30 '14
Python 3; as always, feedback and criticism welcome:
Output example:
1
u/LyndonArmitage Jul 30 '14 edited Jul 30 '14
I've done mine in Java.
I tried to implement a grid that expands as much as needed and the ability to follow any length of rules (although I haven't guaranteed what the colours will be like after a set amount).
It was initially pretty slow when run using the same rules (LRRRRRLLR) and iterations (3000000) as /u/Elite6809's solution because I was iterating over an ever increasing ArrayList instead of using a map/linked list. After changing it quickly to use a Map the speed increased exponentially, to the point where doing 100000 iterations took about 0.04 seconds instead of 1.45 seconds, and doing the 3000000 iterations takes around half a second.
I split it across two Java Classes with one or two inner classes: Github Gist. It should compile in Java 7 and up.
Example output in an imugur album includes using the same rules Elite used (as they make some nice examples) as well as the original black and white rules.
0
u/[deleted] Jul 30 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 30 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 30 '14
[deleted] — view deleted comment