r/dailyprogrammer • u/fvandepitte 0 0 • Jan 26 '17
[2017-01-26] Challenge #300 [Easy/Intermediate] Let's make some noise part 2
Description
Now that we have the basic, let's review something else Elementary cellular automaton
I could explain it, but over at Wolfram they do a pretty decent job.
Formal Inputs & Outputs
All tapes have 1 active cell at the center
Input description
As input you recieve 3 values:
- the size of the tape/array
- the number of rows to output
- the number of the rule
Example 1
43 40 2Example 2
43 17 90Output description
Example 1
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* Example 2
*
* *
* *
* * * *
* *
* * * *
* * * *
* * * * * * * *
* *
* * * *
* * * *
* * * * * * * *
* * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * * * * * * * * * Bonus
Add 2 rules by a logic opperator (and, or, nor, nand, xor, xnor).
For this you keep both outputs in memory and only the output goes trough the logic comparison for output.
Examples will be added later
Notes/Hints
I know this has been done before and this isn't very new... but it will all come together at the last challenge this week.
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
2
u/Godspiral 3 3 Jan 26 '17
example 2 input missing?
Also, the start input is a 1 at the center of the "tape"?
22
u/lukz 2 0 Jan 26 '17
Game boy assembly
I am using the full screen of the game boy, which is 20x18 characters. Here is a screenshot.
Program size is 57 bytes.
5
u/skeeto -9 8 Jan 26 '17 edited Jan 26 '17
C and make, in a totally convoluted (but fun!) solution. The C program is a metaprogram that produces a Makefile which uses
makecompute the cellular automaton. This is because Makefile assignments are turing complete. Example usage:Here's the Makefile for example 2: noise.mak. You can run that with your favorite
makeon a unix system. The file must be namednoise.maksince that name is hardcoded inside the Makefile itself.The article I linked goes into all the details, but here's a rough summary. The rule is turned into a table like this (rule 90):
I used "@" and "." since space isn't a valid identifier character for
make. They're translated when printed. The initial state looks like this (snippet from the middle). Each cell is named by its numeric index.The next state for each is computed from a lookup into the table, formed by concatenating the states of the inputs. Note the double-dereference.
There's a countdown table, which is kind of complicated. The article explains how this works. It's used to terminate the loop.
The output line is assembled:
And the next set of arguments is assembled:
To loop for the next line, the Makefile invokes itself recursively.
Edit: Add a sleep and a lot of rows and you have a really cool animation: (input: 79 4000 90) noise.mak.
Finally here's my code.
4
u/obnoxiouslyraven Jan 26 '17 edited Jan 26 '17
C#
This is my first non-"hello world" in c#. I usually do java and the difference seems minimal so far. Feedback appreciated.
edit: after looking again at the example 1 output, I think it's supposed to wrap around to the other side when it attempts to read a column too large/small in the previous generation. I misunderstood that before but fixed it now.
2
u/Scroph 0 0 Jan 26 '17
C++ :
2
u/iDownvoteBlink182 Jan 26 '17
C#. Please take a look and rip it apart so I can learn.
This was actually really fun. I thought it was going to be an ugly mess as I was writing it but when I took a step back and looked it over it actually looked much better than I expected. I'm still not sure if I want to split the logic for creating the next line out into one more method. I'd love to hear a more elegant solution to all those if statements.
1
u/ranDumbProgrammer Jan 26 '17 edited Jan 26 '17
C#
1
u/[deleted] Jan 27 '17 edited Jan 27 '17
PYTHON 3
Not quite as fancy as some other solutions, my knowledge of some of the more complex parts of python is more limited than some....
1
u/wutaki 0 1 Jan 27 '17 edited Jan 27 '17
Python 3 with numpy (not needed though)
1
u/fecal_brunch Jan 28 '17
Rust:
This is my second attempt at writing a Rust program, comments and criticism invited.
1
u/FrankRuben27 0 1 Jan 28 '17
In Forth (tested with gforth):
1
u/skeeto -9 8 Jan 28 '17 edited Jan 29 '17
C as a straightforward solution since I need it for the next part.
1
u/krisztian_mikail Jan 29 '17
Code written in C.Not really efficient, just testing my hand at dynamic memory allocation.
include <stdlib.h>
include <stdio.h>
int** CreateMatrix (int rows, int columns) { int matrix; int row,column; matrix=(int) malloc (rowssizeof (int)); for (row=0;row<rows;row++) { matrix [row]=(int*) malloc (columns*sizeof(int)); } for (row=0;row<rows;row++) for (column=0;column<columns;column++) matrix [row][column]=0; matrix [0][columns/2]=1; return matrix;
} void FreeMatrix(int** matrix,int rows,int columns) { int row,column; for (row=0;row<rows;row++) { free (matrix [row]); } free (matrix); } int *ConvertToBinary(int number) { int static binaryEquivalent[] ={0,0,0,0,0,0,0,0}; int index=7; while (number !=0) { binaryEquivalent[index]=number%2; number=number/2; index--; } return binaryEquivalent; }
int** FollowRule (int** matrix, int *binaryEquivalent,int rows, int columns) { int row, column; for (row=1;row<rows;row++) { matrix [row][columns-1]=binaryEquivalent[7-(matrix [row-1][columns-2]*4+matrix[row-1][columns-1]*2+matrix[row-1][0])]; matrix [row][0]=binaryEquivalent[7-(matrix [row-1][columns-1]*4+matrix[row-1][0]*2+matrix[row-1][1])]; for (column=1;column<columns-1;column++) { matrix [row][column]=binaryEquivalent[7-(matrix [row-1][column-1]*4+matrix[row-1][column]*2+matrix[row-1][column+1])]; } } return matrix; } int main (void) { int **matrix; int *binaryEquivalent; int rows,columns,row,column;
}
1
u/SimonReiser Jan 31 '17
C++ without bonus
If a negative number for the amount of rows is used, then the program will output everything with a delay (so you can watch the magic in a terminal).
1
u/moanzie Feb 01 '17
Java. Feedback is welcome.
1
u/ff8c00 Feb 02 '17 edited Feb 10 '17
C# Gist
I knew nothing about elementary cellular automaton before I did this challenge. Very interesting. Thank you.
1
u/[deleted] Feb 04 '17
R
1
u/Godspiral 3 3 Jan 26 '17 edited Jan 26 '17
in J,
0
u/[deleted] Jan 26 '17
[deleted] — view deleted comment