One-dimensional cellular automata: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 147:
Generation 9 __##________________
</pre>
 
=={{header|ALGOL 68}}==
===Using the low level packed arrays of BITS manipulation operators===
Line 724 ⟶ 725:
11000000100000000001
11000000000000000001</pre>
 
=={{header|C}}==
<lang c>#include <stdio.h>
Line 791 ⟶ 793:
do { printf(c + 1); } while (evolve(c + 1, sizeof(c) - 3));
return 0;
}</lang>
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Collections.Generic;
 
namespace prog
{
class MainClass
{
const int n_iter = 10;
static int[] f = { 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 };
public static void Main (string[] args)
{
for( int i=0; i<f.Length; i++ )
Console.Write( f[i]==0 ? "-" : "#" );
Console.WriteLine("");
int[] g = new int[f.Length];
for( int n=n_iter; n!=0; n-- )
{
for( int i=1; i<f.Length-1; i++ )
{
if ( (f[i-1] ^ f[i+1]) == 1 ) g[i] = f[i];
else if ( f[i] == 0 && (f[i-1] & f[i+1]) == 1 ) g[i] = 1;
else g[i] = 0;
}
g[0] = ( (f[0] & f[1]) == 1 ) ? 1 : 0;
g[g.Length-1] = ( (f[f.Length-1] & f[f.Length-2]) == 1 ) ? 1 : 0;
int[] tmp = f;
f = g;
g = tmp;
for( int i=0; i<f.Length; i++ )
Console.Write( f[i]==0 ? "-" : "#" );
Console.WriteLine("");
}
}
}
}</lang>
 
Line 836 ⟶ 879:
__##________________
__##________________</pre>
 
=={{header|C sharp}}==
<lang csharp>using System;
using System.Collections.Generic;
 
namespace prog
{
class MainClass
{
const int n_iter = 10;
static int[] f = { 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 };
public static void Main (string[] args)
{
for( int i=0; i<f.Length; i++ )
Console.Write( f[i]==0 ? "-" : "#" );
Console.WriteLine("");
int[] g = new int[f.Length];
for( int n=n_iter; n!=0; n-- )
{
for( int i=1; i<f.Length-1; i++ )
{
if ( (f[i-1] ^ f[i+1]) == 1 ) g[i] = f[i];
else if ( f[i] == 0 && (f[i-1] & f[i+1]) == 1 ) g[i] = 1;
else g[i] = 0;
}
g[0] = ( (f[0] & f[1]) == 1 ) ? 1 : 0;
g[g.Length-1] = ( (f[f.Length-1] & f[f.Length-2]) == 1 ) ? 1 : 0;
int[] tmp = f;
f = g;
g = tmp;
for( int i=0; i<f.Length; i++ )
Console.Write( f[i]==0 ? "-" : "#" );
Console.WriteLine("");
}
}
}
}</lang>
 
=={{header|Ceylon}}==
Line 1,348 ⟶ 1,350:
}</lang>
The output is the same as the second version.
 
=={{header|DWScript}}==
<lang delphi>const ngenerations = 10;
const table = [0, 0, 0, 1, 0, 1, 1, 0];
 
var a := [0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0];
var b := a;
 
var i, j : Integer;
for i := 1 to ngenerations do begin
for j := a.low+1 to a.high-1 do begin
if a[j] = 0 then
Print('_')
else Print('#');
var val := (a[j-1] shl 2) or (a[j] shl 1) or a[j+1];
b[j] := table[val];
end;
var tmp := a;
a := b;
b := tmp;
PrintLn('');
end;
</lang>
{{out}}
<pre>_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________
__##________________</pre>
 
=={{header|Déjà Vu}}==
Line 1,397 ⟶ 1,433:
000000000000000110000000000000000001100011000000000000000000
000000000000000110000000000000000001100011000000000000000000</pre>
 
=={{header|DWScript}}==
<lang delphi>const ngenerations = 10;
const table = [0, 0, 0, 1, 0, 1, 1, 0];
 
var a := [0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0];
var b := a;
 
var i, j : Integer;
for i := 1 to ngenerations do begin
for j := a.low+1 to a.high-1 do begin
if a[j] = 0 then
Print('_')
else Print('#');
var val := (a[j-1] shl 2) or (a[j] shl 1) or a[j+1];
b[j] := table[val];
end;
var tmp := a;
a := b;
b := tmp;
PrintLn('');
end;
</lang>
{{out}}
<pre>_###_##_#_#_#_#__#__
_#_#####_#_#_#______
__##___##_#_#_______
__##___###_#________
__##___#_##_________
__##____###_________
__##____#_#_________
__##_____#__________
__##________________
__##________________</pre>
 
 
=={{header|E}}==
Line 1,604 ⟶ 1,605:
Generation 9: ..##................
</pre>
 
=={{header|Elm}}==
<lang elm>import Maybe exposing (withDefault)
Line 1,697 ⟶ 1,699:
 
Link to live demo: https://dc25.github.io/oneDimensionalCellularAutomataElm/
 
=={{header|Erlang}}==
<lang erlang>
Line 2,490 ⟶ 2,493:
__##_____#__________
__##________________</lang>
 
 
=={{header|Java}}==
Line 2,618 ⟶ 2,620:
<lang javascript>alert(caStep([0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]));</lang>
shows an alert with "0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,0,0,0,0,0".
 
 
=={{header|jq}}==
Line 3,014 ⟶ 3,015:
__##________________
...</pre>
 
 
=={{header|Modula-3}}==
Line 3,595:
__##_____#__________
__##________________</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2014-02-27}}
We'll make a general algorithm capable of computing any cellular automata
as defined by [[wp:Stephen Wolfram|Stephen Wolfram]]'s
famous book ''[[wp:A new kind of Science|A new kind of Science]]''.
We will take the liberty of wrapping the array of cells
as it does not affect the result much
and it makes the implementation a lot easier.
 
<lang perl6>class Automaton {
has $.rule;
has @.cells;
has @.code = $!rule.fmt('%08b').flip.comb».Int;
method gist { "|{ @!cells.map({+$_ ?? '#' !! ' '}).join }|" }
method succ {
self.new: :$!rule, :@!code, :cells(
@!code[
4 «*« @!cells.rotate(-1)
»+« 2 «*« @!cells
»+« @!cells.rotate(1)
]
)
}
}
 
# The rule proposed for this task is rule 0b01101000 = 104
 
my @padding = 0 xx 5;
my Automaton $a .= new:
rule => 104,
cells => flat @padding, '111011010101'.comb, @padding
;
say $a++ for ^10;
 
 
# Rule 104 is not particularly interesting so here is [[wp:Rule 90|Rule 90]],
# which shows a [[wp:Sierpinski Triangle|Sierpinski Triangle]].
 
say '';
@padding = 0 xx 25;
$a = Automaton.new: :rule(90), :cells(flat @padding, 1, @padding);
say $a++ for ^20;</lang>
 
{{out}}
<pre>
| ### ## # # # |
| # ##### # # |
| ## ## # |
| ## ### |
| ## # # |
| ## # |
| ## |
| ## |
| ## |
| ## |
 
| # |
| # # |
| # # |
| # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
| # # # # |
| # # # # # # # # |
| # # # # # # # # |
| # # # # # # # # # # # # # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
</pre>
 
=={{header|Phix}}==
Line 4,382 ⟶ 4,301:
(draw-automaton 104 '(0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0) 10)</lang>
 
=={{header|REXXRaku}}==
(formerly Perl 6)
This REXX version will show (as a default) &nbsp; 40 &nbsp; generations, &nbsp; or less if the generations of cellular automata repeat.
{{works with|rakudo|2014-02-27}}
<lang rexx>/*REXX program generates & displays N generations of one─dimensional cellular automata. */
We'll make a general algorithm capable of computing any cellular automata
parse arg $ gens . /*obtain optional arguments from the CL*/
as defined by [[wp:Stephen Wolfram|Stephen Wolfram]]'s
if $=='' | $=="," then $=001110110101010 /*Not specified? Then use the default.*/
famous book ''[[wp:A new kind of Science|A new kind of Science]]''.
if gens=='' | gens=="," then gens=40 /* " " " " " " */
We will take the liberty of wrapping the array of cells
as it does not affect the result much
and it makes the implementation a lot easier.
 
<lang perl6>class Automaton {
do #=0 for gens /* process the one-dimensional cells.*/
has $.rule;
say " generation" right(#,length(gens)) ' ' translate($, "#·", 10)
has @.cells;
@=0 /* [↓] generation.*/
has @.code = $!rule.fmt('%08b').flip.comb».Int;
do j=2 for length($) - 1; x=substr($, j-1, 3) /*obtain the cell.*/
if x==011 | x==101 | x==110 then @=overlay(1, @, j) /*the cell lives. */
method gist { "|{ @!cells.map({+$_ ?? '#' !! ' '}).join }|" }
else @=overlay(0, @, j) /* " " dies. */
end /*j*/
method succ {
self.new: :$!rule, :@!code, :cells(
@!code[
4 «*« @!cells.rotate(-1)
»+« 2 «*« @!cells
»+« @!cells.rotate(1)
]
)
}
}
 
# The rule proposed for this task is rule 0b01101000 = 104
if $==@ then do; say right('repeats', 40); leave; end /*does it repeat? */
 
$=@ /*now use the next generation of cells.*/
my @padding = 0 xx 5;
end /*#*/ /*stick a fork in it, we're all done. */</lang>
my Automaton $a .= new:
'''output''' when using the default input:
rule => 104,
cells => flat @padding, '111011010101'.comb, @padding
;
say $a++ for ^10;
 
 
# Rule 104 is not particularly interesting so here is [[wp:Rule 90|Rule 90]],
# which shows a [[wp:Sierpinski Triangle|Sierpinski Triangle]].
 
say '';
@padding = 0 xx 25;
$a = Automaton.new: :rule(90), :cells(flat @padding, 1, @padding);
say $a++ for ^20;</lang>
 
{{out}}
<pre>
generation| 0 ··###· ##· #· #· #· |
generation| 1 ··#· #####· #· #·· |
generation| 2 ···##··· ##· #··· |
generation| 3 ···##··· ###···· |
generation| 4 ···##··· #· #···· |
| ## # |
generation 5 ···##····#·····
| ## |
generation 6 ···##··········
| ## repeats|
| ## |
| ## |
 
| # |
| # # |
| # # |
| # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
| # # # # |
| # # # # # # # # |
| # # # # # # # # |
| # # # # # # # # # # # # # # # # |
| # # |
| # # # # |
| # # # # |
| # # # # # # # # |
</pre>
 
Line 4,569 ⟶ 4,540:
#10 generations
~~~</lang>
 
=={{header|REXX}}==
This REXX version will show (as a default) &nbsp; 40 &nbsp; generations, &nbsp; or less if the generations of cellular automata repeat.
<lang rexx>/*REXX program generates & displays N generations of one─dimensional cellular automata. */
parse arg $ gens . /*obtain optional arguments from the CL*/
if $=='' | $=="," then $=001110110101010 /*Not specified? Then use the default.*/
if gens=='' | gens=="," then gens=40 /* " " " " " " */
 
do #=0 for gens /* process the one-dimensional cells.*/
say " generation" right(#,length(gens)) ' ' translate($, "#·", 10)
@=0 /* [↓] generation.*/
do j=2 for length($) - 1; x=substr($, j-1, 3) /*obtain the cell.*/
if x==011 | x==101 | x==110 then @=overlay(1, @, j) /*the cell lives. */
else @=overlay(0, @, j) /* " " dies. */
end /*j*/
 
if $==@ then do; say right('repeats', 40); leave; end /*does it repeat? */
$=@ /*now use the next generation of cells.*/
end /*#*/ /*stick a fork in it, we're all done. */</lang>
'''output''' when using the default input:
<pre>
generation 0 ··###·##·#·#·#·
generation 1 ··#·#####·#·#··
generation 2 ···##···##·#···
generation 3 ···##···###····
generation 4 ···##···#·#····
generation 5 ···##····#·····
generation 6 ···##··········
repeats
</pre>
 
=={{header|Ring}}==
Line 4,782 ⟶ 4,783:
__##________________
__##________________
</pre> =={{header|Seed7}}==
A graphical cellular automaton can be found [http://seed7.sourceforge.net/algorith/graphic.htm#cellauto here].
 
> petriCache(i) Then stable = False
If petriCache(i) Then dead = False
Next
 
PetriDish = petriCache
 
If dead Then Return PetriStatus.Dead
If stable Then Return PetriStatus.Stable
Return PetriStatus.Active
 
End Function
 
Private Function BuildDishString(ByVal PetriDish As BitArray) As String
Dim sw As New StringBuilder()
For Each b As Boolean In PetriDish
sw.Append(IIf(b, "#", "_"))
Next
 
Return sw.ToString()
End Function
End Module
 
=={{header|SequenceL}}==
Line 4,976 ⟶ 5,000:
..##..............
..##..............</pre>
 
 
=={{header|Vedit macro language}}==
Line 5,310 ⟶ 5,333:
</pre>
/pre>
 
=={{header|Seed7}}==
A graphical cellular automaton can be found [http://seed7.sourceforge.net/algorith/graphic.htm#cellauto here].
 
> petriCache(i) Then stable = False
If petriCache(i) Then dead = False
Next
 
PetriDish = petriCache
 
If dead Then Return PetriStatus.Dead
If stable Then Return PetriStatus.Stable
Return PetriStatus.Active
 
End Function
 
Private Function BuildDishString(ByVal PetriDish As BitArray) As String
Dim sw As New StringBuilder()
For Each b As Boolean In PetriDish
sw.Append(IIf(b, "#", "_"))
Next
 
Return sw.ToString()
End Function
End Module
10,333

edits