r/dailyprogrammer • u/Steve132 0 1 • Jul 04 '12
[7/4/2012] Challenge #72 [easy]
The one-dimensional simple cellular automata [Rule 110] (http://en.wikipedia.org/wiki/Rule_110) is the only such cellular automata currently known to be turing-complete, and many people say it is the simplest known turing-complete system.
Implement a program capable of outputting an ascii-art representation of applying Rule 110 to some initial state. How many iterations and what your initial state is is up to you!
You may chose to implement rule 124 instead if you like (which is the same thing, albeit backwards).
Bonus points if your program can take an arbitrary rule integer from 0-255 as input and run that rule instead!
4
u/[deleted] Jul 04 '12
Could someone explain how cellular automata behave at their boundaries?
(?[1]0)1101010101010110(0[0]?)
I can't seem to figure out how to handle these, and wikipedia isn't very helpful.
6
u/drb226 0 0 Jul 04 '12 edited Jul 05 '12
Time for some knot-tying!
Well, first, let's start off with a simple datatype to represent "bits".
OK, now a type to represent a cell. Cells have two neighbors and a value.
Computations involving a cell's bit value should be straightforward, so I've made that field strict. The neighbor fields, however, will need to be lazy in order to tie the knot as we shall soon see. Basically, I want this to be a circular doubly-linked list. But we need to be able to have some notion of when we have gone all the way around the "loop", so we'll wrap up our cells in another data type to keep our bearings:
A CellLoop chooses a definitive "starting point" cell, and contains the "length" of the loop.
Now, given a list of Bits, we want to be able to create a CellLoop. We'll do that by tying the knot like so:
fromList' takes three inputs: the list of bits, the "previous" cell of the completed loop, and the "first" cell of the completed loop. It has two outputs: the "first" and "last" cells of the completed loop, respectively. In the base case, you can see that it simply regurgitates its inputs. In the interesting case,
thisandnextare defined with mutual recursion, and letrec magic ties them together.Converting back to a list of bits is much easier, we just use the
lengththat we stored as "fuel", and when the fuel runs out, we stop.Now, we actually want a CellLoop to display a little differently than just a list of Bits, so we'll make a show instance accordingly:
Now for the final hurdle: evolution. We'd like to write a function
evolve :: CellLoop -> CellLoop. In order to do so, we'll use both of the tricks we used previously: tying the knot, and fuel.Since a Cell always knows about its neighbors, the computation of the evolved cell value can be completely separate from the code that traverses and reconstructs the CellLoop structure.
It should be straightforward, given a technique to turn an integer into a list of bits, to parameterize
evolveCellVal(and by extension,evolve) on any rule. This is left as an exercise to the reader. :)[edit] I've been playing around with BlogLiterately lately, so I took a little extra time and converted this comment into a full-fledged blog post: http://unknownparallel.wordpress.com/2012/07/04/rule-110/
Source code available: https://github.com/DanBurton/Blog/blob/master/Literate%20Haskell/rule110.lhs
1
u/Cosmologicon 2 3 Jul 04 '12
bc (outputs a PBM):
resulting image
Incidentally, it's not the case that this is the only CA known to be Turing complete. The Game of Life is also. Maybe you mean it's the only of Wolfram's "Rules" that's Turing complete?
1
u/robotfarts Jul 04 '12 edited Jul 04 '12
1
u/sleepingsquirrel Jul 04 '12
Perl/Tk
1
u/ixid 0 0 Jul 04 '12 edited Jul 05 '12
D language, bonus points version. It's amazing to see the space ships pattern forming, I'm just using a break point to draw it line by line in the console. The automata wraps around start to end to form a loop. It retains the previous two bits and bitshifts so only checks one bit per new bit written, writing about 40.5 million cells per second on a 1.6 GHz Core II Duo on a 231 cell automata.
Is 18 making Sierpinski Triangles? Looks like it. 22 and 26 are doing it too. 30 makes makes very complex patterns.
1
u/bh3 Jul 05 '12
Python (bonus):
1
u/rxst Jul 05 '12
Java, bonus version.
1
u/j0z Jul 05 '12
C# version:
1
u/JensjD Jul 06 '12 edited Jul 06 '12
(python3) woh, this was a tough one for me. i am not sure if this is correct. but it is what i was trying to do.
1
u/[deleted] Jul 16 '12 edited Jul 16 '12
Better late than never! Python, applies any rule to any initial state for however many iterations.
If anyone reads this, is there a way to implement a feature whereby the script detects if there is enough space to print the output? For example, the default width of cmd.exe is 80 characters, right? So is there a way for the script to warn the user that the output will exceed 80 characters?
Also, Here's a bunch of states to try. Most of them stabilize pretty quickly apparently.
1
u/mktange Jul 17 '12
A compact Python version with bonus implemented. It also has size and alignment arguments (align the initial state either right or left).
It operates with a string of 0's and 1's, but converts it to spaces and X's when printing.
1
u/cdelahousse Jul 29 '12
Javascript
automata(rule110);