1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: bit-arrays io kernel locals math sequences ;
4 IN: rosetta-code.one-d-cellular
6 ! http://rosettacode.org/wiki/One-dimensional_cellular_automata
8 ! Assume an array of cells with an initial distribution of live
9 ! and dead cells, and imaginary cells off the end of the array
10 ! having fixed values.
12 ! Cells in the next generation of the array are calculated based
13 ! on the value of the cell and its left and right nearest
14 ! neighbours in the current generation. If, in the following
15 ! table, a live cell is represented by 1 and a dead cell by 0 then
16 ! to generate the value of the cell at a particular index in the
17 ! array of cellular values you use the following table:
21 ! 010 -> 0 # Dies without enough neighbours
22 ! 011 -> 1 # Needs one neighbour to survive
24 ! 101 -> 1 # Two neighbours giving birth
25 ! 110 -> 1 # Needs one neighbour to survive
26 ! 111 -> 0 # Starved to death.
28 : bool-sum ( bool1 bool2 -- sum )
30 [ [ 1 ] [ 0 ] if ] if ;
32 :: neighbours ( index world -- # )
33 index [ 1 - ] [ 1 + ] bi [ world ?nth ] bi@ bool-sum ;
35 : count-neighbours ( world -- neighbours )
36 [ length iota ] keep [ neighbours ] curry map ;
38 : life-law ( alive? neighbours -- alive? )
39 swap [ 1 = ] [ 2 = ] if ;
41 : step ( world -- world' )
42 dup count-neighbours [ life-law ] ?{ } 2map-as ;
44 : print-cellular ( world -- )
45 [ CHAR: # CHAR: _ ? ] "" map-as print ;
47 : main-cellular ( -- )
48 ?{ f t t t f t t f t f t f t f t f f t f f }
49 10 [ dup print-cellular step ] times print-cellular ;