The Challenge: Write the shortest program that implements John H. Conway's Game of Life cellular automaton. [link]
EDIT: After about a week of competition, I have selected a victor: pdehaan, for managing to beat the Matlab solution by one character with perl.
For those who haven't heard of Game of Life, you take a grid (ideally infinite) of square cells. Cells can be alive (filled) or dead (empty). We determine which cells are alive in the next step of time by applying the following rules:
- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
Your program will read in a 40x80 character ASCII text file specified as a command-line argument, as well as the number of iterations (N) to perform. Finally, it will output to an ASCII file out.txt the state of the system after N iterations.
Here is an example run with relevant files:
in.txt:
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
..................................XX............................................
..................................X.............................................
.......................................X........................................
................................XXXXXX.X........................................
................................X...............................................
.................................XX.XX...XX.....................................
..................................X.X....X.X....................................
..................................X.X......X....................................
...................................X.......XX...................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
Iterate 100 times:
Q:\>life in.txt 100
Resultant Output (out.txt)
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
..................................XX............................................
..................................X.X...........................................
....................................X...........................................
................................XXXXX.XX........................................
................................X.....X.........................................
.................................XX.XX...XX.....................................
..................................X.X....X.X....................................
..................................X.X......X....................................
...................................X.......XX...................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
................................................................................
The Rules:
- You need to use file I/O to read/write the files.
- You need to accept an input file and the number of iterations as arguments
- You need to generate out.txt (overwrite if it exists) in the specified format
- You don't need to deal with the edges of the board (wraparound, infinite grids .etc)
- EDIT: You do need to have newlines in your output file.
The winner will be determined by character count.
Good luck!
Another Java attempt, 361 chars
And a little more readable
Very similar to Molehill’s version. I've tried to use a different FileWriter and to count the cell's neighbors without an additional variable. Unfortunately,
RandomAccessFile
is a pretty long name and it is required that you pass an file access mode.MATLAB 7.8.0 (R2009a) -
174171161150138131128124 charactersFunction syntax: (124 characters)
Here's the easier-to-read version (with unnecessary newlines and whitespace added for better formatting):
And here's how the program is run from the MATLAB Command Window:
Command syntax: (130 characters)
After a comment about calling functions with a command syntax, I dug a little deeper and found out that MATLAB functions can in fact be invoked with a command-line format (with some restrictions). You learn something new every day!
And here's how the program is run from the MATLAB Command Window:
Additional Challenge: Tweetable GIF maker - 136 characters
I thought for fun I'd see if I could dump the output to a GIF file instead of a text file, while still keeping the character count below 140 (i.e. "tweetable"). Here's the nicely-formatted code:
Although IMWRITE is supposed to create a GIF that loops infinitely by default, my GIF is only looping once. Perhaps this is a bug that has been fixed in newer versions of MATLAB. So, to make the animation last longer and make the evolution steps easier to see, I left the frame delay at the default value (which seems to be around half a second). Here's the GIF output using the Gosper Glider Gun pattern:
Improvements
b
from a logical (i.e. "boolean") type to a numerical one to get rid of a few conversions.~~b+0
withb/42
, and replaced'same'
with's'
as an argument to CONV2 (and it surprisingly still worked!).b
back to a logical matrix.b
and reworked the logic in the loop to save 1 additional character.cell2mat
withchar
, saving 4 characters. Thanks Eric!Mathematica -
179163154151 charsSpaces added for readability
Invoke with
Animation:
You can also get a graph of the mean population over time:
A nice pattern for generating gliders from Wikipedia
AFAIK Mathematica uses a Cellular Automaton to generate random numbers using Rule 30.
Ruby 1.8:
178175 charsNewlines are significant (although all can be replaced w/ semicolons.)
Edit: fixed the newline issue, and trimmed 3 chars.
Java,
556532517496472433428420418381 charsUpdate 1: replaced 1st
StringBuffer
byAppendable
and 2nd bychar[]
. Saved 24 chars.Update 2: found a shorter way to read file into
char[]
. Saved 15 chars.Update 3: replaced one
if/else
by?:
and mergedchar[]
andint
declarations. Saved 21 chars.Update 4: replaced
(int)f.length()
andc.length
bys
. Saved 24 chars.Update 5: made improvements as per hints of Molehill. Major one was hardcoding the char length so that I could get rid of
File
. Saved 39 chars.Update 6: minor refactoring. Saved 6 chars.
Update 7: replaced
Integer#valueOf()
bynew Integer()
and refactored for loop. Saved 8 chars.Update 8: Improved neighbour calculation. Saved 2 chars.
Update 9: Optimized file reading since file length is already hardcoded. Saved 37 chars.
More readable version:
Closing after writing is absoletely mandatory, else the file is left empty. It would otherwise have saved another 21 chars.
Further I could also save one more char when I use
46
instead of'.'
, but both javac and Eclipse jerks with a compilation error Possible loss of precision. Weird stuff.Note: this expects an input file with
\n
newlines, not\r\n
as Windows by default uses!R 340 chars
I feel it's slightly cheating to have an add in package that does the actual automata for you, but I'm going with it cos I still had to thrash around with matricies and stuff to read in the file with 'X' instead of 1.
This is my first 'code golf', interesting....