Hacker News new | past | comments | ask | show | jobs | submit login
Game of Life in all languages (rosettacode.org)
7 points by luminaobscura on May 10, 2012 | hide | past | favorite | 5 comments



I don't see a shell version, so below is mine (well...it is not strictly shell, as it uses sort, uniq, grep, sed, and comm). It's written as a filter that takes as input live cell coordinates (one cell per line, X and Y coordinates separated by white space), and outputs the live cells of the next generation.

For example, on this input:

   -10 100
   -10 101
   -10 102
   1000 5
   1001 5
   1002 5
the output is:

   -11 101
   -9 101
   1001 4
   1001 6
   -10 101
   1001 5
Here is the code, for bash or similar shells:

    > alive.$$
    while read cells
    do
        echo $cells >> alive.$$
        set x $cells
        x=$2
        y=$3
        echo $x $((y-1))
        echo $x $((y+1))
        echo $((x-1)) $((y-1))
        echo $((x-1)) $y
        echo $((x-1)) $((y+1))
        echo $((x+1)) $((y-1))
        echo $((x+1)) $y
        echo $((x+1)) $((y+1))
    done | sort | uniq -c > neighbors.$$
    grep '^ *3' < neighbors.$$ | sed -e 's/^ *[0-9].//'
    grep '^ *2' < neighbors.$$ | sed -e 's/^ *[0-9].//' > has2.$$
    sort alive.$$ -o alive.$$
    comm -12 has2.$$ alive.$$
    rm has2.$$ neighbors.$$ alive.$$


If Hello World is inadequate, what canonical program would better serve to demonstrate the salient features of a programming language?

    Must it use variables?
    Must it have flow control?
    Must it actually compute something?
    Must it use non-primitive data structures?
    Must it exhibit use of the language’s code organisation features?
These are some of the more generic properties that programs can have.

...

One suggestion (from David Pearce) was Conway’s Game of Life: a simple simulation of finite automata. This seems like a good demonstration of most of the properties.

http://ejrh.wordpress.com/2012/05/10/beyond-hello-world/


Brainfuck? Erlang? Coldfusion? Visual Basic (6/.NET)? Those are just a couple right off the top of my head.

It is quite cool, though, to see a common problem solved in such a wide variety of languages.

s/all languages/lots of languages/g


The APL version is mindbendingly awesome: http://www.youtube.com/watch?v=a9xAKttWgP4 <-- watch it!


clojure, f# and python seems very terse.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: