One-dimensional cellular automata: Difference between revisions

Content added Content deleted
No edit summary
m (Fixed lang tags.)
Line 15: Line 15:


=={{header|Ada}}==
=={{header|Ada}}==
<lang ada>
<lang ada>with Ada.Text_IO; use Ada.Text_IO;
with Ada.Text_IO; use Ada.Text_IO;


procedure Cellular_Automata is
procedure Cellular_Automata is
Line 57: Line 56:
Step (Culture);
Step (Culture);
end loop;
end loop;
end Cellular_Automata;
end Cellular_Automata;</lang>
</lang>
The implementation defines Petri dish type with Boolean items identifying whether a place is occupied by a living cell. State transition is determined by a simple Boolean expression of three arguments. Sample output:
The implementation defines Petri dish type with Boolean items identifying whether a place is occupied by a living cell. State transition is determined by a simple Boolean expression of three arguments. Sample output:
<pre>
<pre>
Line 74: Line 72:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
===Using the low level packed arrays of BITS manipulation operators===
===Using the low level packed arrays of BITS manipulation operators===
<lang algol68>INT stop generation = 9;
<pre>
INT stop generation = 9;
INT universe width = 20;
INT universe width = 20;
FORMAT alive or dead = $b("#","_")$;
FORMAT alive or dead = $b("#","_")$;
Line 117: Line 114:
FI;
FI;
universe := next universe
universe := next universe
OD
OD</lang>
</pre>
===Using high level BOOL arrays===
===Using high level BOOL arrays===
<lang algol68>INT stop generation = 9;
<pre>
INT stop generation = 9;
INT upb universe = 20;
INT upb universe = 20;
FORMAT alive or dead = $b("#","_")$;
FORMAT alive or dead = $b("#","_")$;
Line 155: Line 150:
next universe[UPB universe] := couple(universe[UPB universe - 1: ]);
next universe[UPB universe] := couple(universe[UPB universe - 1: ]);
universe := next universe
universe := next universe
OD
OD</lang>
</pre>
Output:
Output:
<lang algol68>Generation 0: _###_##_#_#_#_#__#__
<pre>
Generation 0: _###_##_#_#_#_#__#__
Generation 1: _#_#####_#_#_#______
Generation 1: _#_#####_#_#_#______
Generation 2: __##___##_#_#_______
Generation 2: __##___##_#_#_______
Line 168: Line 161:
Generation 7: __##_____#__________
Generation 7: __##_____#__________
Generation 8: __##________________
Generation 8: __##________________
Generation 9: __##________________
Generation 9: __##________________</lang>
</pre>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
ahk [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=147 discussion]
ahk [http://www.autohotkey.com/forum/viewtopic.php?t=44657&postdays=0&postorder=asc&start=147 discussion]
Line 382: Line 374:
}
}
return 0;
return 0;
}</lang>
}
</lang>


The output is:
The output is:
Line 501: Line 492:


=={{header|Forth}}==
=={{header|Forth}}==
: init ( bits count -- )
<lang forth>: init ( bits count -- )
0 do dup 1 and c, 2/ loop drop ;
0 do dup 1 and c, 2/ loop drop ;
20 constant size
create state $2556e size init 0 c,
: .state
cr size 0 do
state i + c@ if ." #" else space then
loop ;
: ctable create does> + c@ ;
ctable rules $68 8 init
: gen
state c@ ( window )
size 0 do
2* state i + 1+ c@ or 7 and
dup rules state i + c!
loop drop ;
: life1d ( n -- )
.state 1 do gen .state loop ;


20 constant size
10 life1d
create state $2556e size init 0 c,

: .state
cr size 0 do
state i + c@ if ." #" else space then
loop ;

: ctable create does> + c@ ;
ctable rules $68 8 init

: gen
state c@ ( window )
size 0 do
2* state i + 1+ c@ or 7 and
dup rules state i + c!
loop drop ;

: life1d ( n -- )
.state 1 do gen .state loop ;

10 life1d</lang>


=={{header|Fortran}}==
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
{{works with|Fortran|90 and later}}
<lang fortran> PROGRAM LIFE_1D
<lang fortran>PROGRAM LIFE_1D
IMPLICIT NONE
LOGICAL :: cells(20) = (/ .FALSE., .TRUE., .TRUE., .TRUE., .FALSE., .TRUE., .TRUE., .FALSE., .TRUE., .FALSE., &
.TRUE., .FALSE., .TRUE., .FALSE., .TRUE., .FALSE., .FALSE., .TRUE., .FALSE., .FALSE. /)
INTEGER :: i
IMPLICIT NONE
DO i = 0, 9

WRITE(*, "(A,I0,A)", ADVANCE = "NO") "Generation ", i, ": "
LOGICAL :: cells(20) = (/ .FALSE., .TRUE., .TRUE., .TRUE., .FALSE., .TRUE., .TRUE., .FALSE., .TRUE., .FALSE., &
CALL Drawgen(cells)
.TRUE., .FALSE., .TRUE., .FALSE., .TRUE., .FALSE., .FALSE., .TRUE., .FALSE., .FALSE. /)
CALL Nextgen(cells)
END DO
INTEGER :: i
DO i = 0, 9
CONTAINS
WRITE(*, "(A,I0,A)", ADVANCE = "NO") "Generation ", i, ": "
SUBROUTINE Nextgen(cells)
CALL Drawgen(cells)
LOGICAL, INTENT (IN OUT) :: cells(:)
CALL Nextgen(cells)
END DO
LOGICAL :: left, centre, right

INTEGER :: i
CONTAINS

left = .FALSE.
DO i = 1, SIZE(cells)-1
SUBROUTINE Nextgen(cells)
centre = cells(i)
LOGICAL, INTENT (IN OUT) :: cells(:)
right = cells(i+1)
LOGICAL :: left, centre, right
INTEGER :: i
IF (left .AND. right) THEN
cells(i) = .NOT. cells(i)
ELSE IF (.NOT. left .AND. .NOT. right) THEN
cells(i) = .FALSE.
END IF
left = centre
END DO
cells(SIZE(cells)) = left .AND. right
END SUBROUTINE Nextgen
SUBROUTINE Drawgen(cells)
LOGICAL, INTENT (IN OUT) :: cells(:)
INTEGER :: i
DO i = 1, SIZE(cells)
IF (cells(i)) THEN
WRITE(*, "(A)", ADVANCE = "NO") "#"
ELSE
WRITE(*, "(A)", ADVANCE = "NO") "_"
END IF
END DO
WRITE(*,*)
END SUBROUTINE Drawgen
left = .FALSE.
END PROGRAM LIFE_1D</lang>
DO i = 1, SIZE(cells)-1
centre = cells(i)
right = cells(i+1)
IF (left .AND. right) THEN
cells(i) = .NOT. cells(i)
ELSE IF (.NOT. left .AND. .NOT. right) THEN
cells(i) = .FALSE.
END IF
left = centre
END DO
cells(SIZE(cells)) = left .AND. right
END SUBROUTINE Nextgen

SUBROUTINE Drawgen(cells)
LOGICAL, INTENT (IN OUT) :: cells(:)
INTEGER :: i
DO i = 1, SIZE(cells)
IF (cells(i)) THEN
WRITE(*, "(A)", ADVANCE = "NO") "#"
ELSE
WRITE(*, "(A)", ADVANCE = "NO") "_"
END IF
END DO
WRITE(*,*)
END SUBROUTINE Drawgen
END PROGRAM LIFE_1D</lang>
Output
Output
Generation 0: _###_##_#_#_#_#__#__
Generation 0: _###_##_#_#_#_#__#__
Line 592: Line 583:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>module Life1D where
<pre>
module Life1D where


import Data.List
import Data.List
Line 616: Line 606:
g <- newStdGen
g <- newStdGen
let oersoep = map ("_#"!!). take 36 $ randomRs(0,1) g
let oersoep = map ("_#"!!). take 36 $ randomRs(0,1) g
mapM_ print . lahmahgaan $ oersoep
mapM_ print . lahmahgaan $ oersoep</lang>
</pre>
Some output:
Some output:
<lang haskell>*Life1D> mapM_ print . lahmahgaan $ "_###_##_#_#_#_#__#__"
<pre>
*Life1D> mapM_ print . lahmahgaan $ "_###_##_#_#_#_#__#__"
"_###_##_#_#_#_#__#__"
"_###_##_#_#_#_#__#__"
"_#_#####_#_#_#______"
"_#_#####_#_#_#______"
Line 638: Line 626:
"________________________________#_#_"
"________________________________#_#_"
"_________________________________#__"
"_________________________________#__"
"____________________________________"
"____________________________________"</lang>
</pre>
=={{header|J}}==
=={{header|J}}==
life1d=: '_#'{~ (3(2=+/\) 0,],0:)^:a:
<lang j>life1d=: '_#'{~ (3(2=+/\) 0,],0:)^:a:</lang>
Example use:
Example use:
life1d ? 20 # 2
<lang j> life1d ? 20 # 2
_###_##_#_#_#_#__#__
_###_##_#_#_#_#__#__
_#_#####_#_#_#______
_#_#####_#_#_#______
__##___##_#_#_______
__##___##_#_#_______
__##___###_#________
__##___###_#________
__##___#_##_________
__##___#_##_________
__##____###_________
__##____###_________
__##____#_#_________
__##____#_#_________
__##_____#__________
__##_____#__________
__##________________
__##________________</lang>


=={{header|Java}}==
=={{header|Java}}==
Line 712: Line 699:
=={{header|Logo}}==
=={{header|Logo}}==
{{works with|UCBLogo}}
{{works with|UCBLogo}}
<lang logo>make "cell_list [0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0]
<lang logo>
make "cell_list [0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0]
make "generations 9
make "generations 9


Line 750: Line 736:
end
end


CA_1D :cell_list :generations
CA_1D :cell_list :generations</lang>
</lang>
Sample Output:
Sample Output:
<pre>
<pre>
Line 767: Line 752:


=={{header|M4}}==
=={{header|M4}}==
<lang M4>
<lang M4>divert(-1)
divert(-1)
define(`set',`define(`$1[$2]',`$3')')
define(`set',`define(`$1[$2]',`$3')')
define(`get',`defn(`$1[$2]')')
define(`get',`defn(`$1[$2]')')
Line 803: Line 787:
for(`j',1,10,
for(`j',1,10,
`show(x)`'evolve(`x',`y')`'swap(`x',x,`y')
`show(x)`'evolve(`x',`y')`'swap(`x',x,`y')
')`'show(x)
')`'show(x)</lang>
</lang>


Output:
Output:
Line 823: Line 806:
=={{header|Mathematica}}==
=={{header|Mathematica}}==
Built-in function:
Built-in function:
<lang Mathematica>CellularAutomaton[{{0,0,_}->0,{0,1,0}->0,{0,1,1}->1,{1,0,0}->0,{1,0,1}->1,{1,1,0}->1,{1,1,1}->0},{{1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1},0},12]
<lang Mathematica>
Print @@@ (% /. {1 -> "#", 0 -> "."});</lang>
CellularAutomaton[{{0,0,_}->0,{0,1,0}->0,{0,1,1}->1,{1,0,0}->0,{1,0,1}->1,{1,1,0}->1,{1,1,1}->0},{{1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1},0},12]
Print @@@ (% /. {1 -> "#", 0 -> "."});
</lang>
gives back:
gives back:
<lang Mathematica>
<lang Mathematica>###.##.#.#.#.#..#
###.##.#.#.#.#..#
#.#####.#.#.#....
#.#####.#.#.#....
.##...##.#.#.....
.##...##.#.#.....
Line 841: Line 821:
.##..............
.##..............
.##..............
.##..............
.##..............
.##..............</lang>
</lang>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 907: Line 886:
=={{header|Nial}}==
=={{header|Nial}}==
(life.nial)
(life.nial)
% we need a way to write a values and pass the same back
<lang nial>% we need a way to write a values and pass the same back
wi is rest link [write, pass]
wi is rest link [write, pass]
% calculate the neighbors by rotating the array left and right and joining them
% calculate the neighbors by rotating the array left and right and joining them
neighbors is pack [pass, sum [-1 rotate, 1 rotate]]
neighbors is pack [pass, sum [-1 rotate, 1 rotate]]
% calculate the individual birth and death of a single array element
% calculate the individual birth and death of a single array element
igen is fork [ = [ + [first, second], 3 first], 0 first, = [ + [first, second], 2 first], 1 first, 0 first ]
igen is fork [ = [ + [first, second], 3 first], 0 first, = [ + [first, second], 2 first], 1 first, 0 first ]
% apply that to the array
% apply that to the array
nextgen is each igen neighbors
nextgen is each igen neighbors
% 42
% 42
life is fork [ > [sum pass, 0 first], life nextgen wi, pass ]
life is fork [ > [sum pass, 0 first], life nextgen wi, pass ]</lang>
Using it
Using it
|loaddefs 'life.nial'
<lang nial>|loaddefs 'life.nial'
|I := [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]
|I := [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]
|life I
|life I</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
Line 952: Line 931:
else print_char '#'
else print_char '#'
done;
done;
print_newline()
print_newline()</lang>
</lang>


put the code above in a file named "life.ml", and then use it in the ocaml toplevel like this:
put the code above in a file named "life.ml", and then use it in the ocaml toplevel like this:
Line 1,011: Line 989:
universe.replace('0', printdead).replace('1', printlive) )
universe.replace('0', printdead).replace('1', printlive) )
universe = offendvalue + universe + offendvalue
universe = offendvalue + universe + offendvalue
universe = ''.join(neighbours2newstate[universe[i:i+3]] for i in range(cellcount))
universe = ''.join(neighbours2newstate[universe[i:i+3]] for i in range(cellcount))</lang>
</lang>
Sample output:
Sample output:
<pre>Generation 0: _###_##_#_#_#_#__#__
<pre>Generation 0: _###_##_#_#_#_#__#__
Line 1,043: Line 1,020:
=={{header|R}}==
=={{header|R}}==


<lang R>set.seed(15797, kind="Mersenne-Twister")
<lang R>
set.seed(15797, kind="Mersenne-Twister")


maxgenerations = 10
maxgenerations = 10
Line 1,076: Line 1,052:
universe <- cellularAutomata(universe, stayingAlive)
universe <- cellularAutomata(universe, stayingAlive)
cat(format(i, width=3), deadOrAlive2string(universe), "\n")
cat(format(i, width=3), deadOrAlive2string(universe), "\n")
}</lang>
}
</lang>


Sample output,
Sample output,
Line 1,201: Line 1,176:


This implementation writes the calculated patterns into an edit buffer, where the results can viewed and saved into a file if required. The edit buffer also acts as storage during calculations.
This implementation writes the calculated patterns into an edit buffer, where the results can viewed and saved into a file if required. The edit buffer also acts as storage during calculations.
<lang vedit>IT("Gen 0: ..###.##.#.#.#.#..#.....") // initial pattern
<pre>
IT("Gen 0: ..###.##.#.#.#.#..#.....") // initial pattern
#9 = Cur_Col
#9 = Cur_Col


Line 1,219: Line 1,193:
IT("Gen ") Num_Ins(#8, LEFT+NOCR) IT(": ")
IT("Gen ") Num_Ins(#8, LEFT+NOCR) IT(": ")
Reg_Ins(20)
Reg_Ins(20)
}</lang>
}
</pre>


Sample output:
Sample output:
<lang vedit>Gen 0: ..###.##.#.#.#.#..#.....
<pre>
Gen 0: ..###.##.#.#.#.#..#.....
Gen 1: ..#.#####.#.#.#.........
Gen 1: ..#.#####.#.#.#.........
Gen 2: ...##...##.#.#..........
Gen 2: ...##...##.#.#..........
Line 1,233: Line 1,205:
Gen 7: ...##.....#.............
Gen 7: ...##.....#.............
Gen 8: ...##...................
Gen 8: ...##...................
Gen 9: ...##...................
Gen 9: ...##...................</lang>
</pre>