One-dimensional cellular automata: Difference between revisions

inserted PAscal
(inserted PAscal)
Line 1,077:
9: ..@@................</pre>
 
=pre>###_##_#_#_#_#__#__
=={{header|CoffeeScript}}==
#_#####_#_#_#______
_##___##_#_#_______
_##___###_#________
_##___#_##_________
_##____###_________
_##____#_#_________
_##_____#__________
_##________________={{header|CoffeeScript}}==
<lang coffeescript>
# We could cheat and count the bits, but let's keep this general.
Line 3,240 ⟶ 3,248:
passed as a 0-1 vector.
<lang parigp>step(v)=my(u=vector(#v),k);u[1]=v[1]&v[2];u[#u]=v[#v]&v[#v-1];for(i=2,#v-1,k=v[i-1]+v[i+1];u[i]=if(v[i],k==1,k==2));u;</lang>
=={{header|Pascal}}==
<lang pascal>program Test;
{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE}{$ENDIF}
uses
sysutils;
const
cCHAR: array[0..1] of char = ('_','#');
type
TRow = array of byte;
//ring buffer
Ttriple = array[0..2] of byte;
 
function Convert(const s:string):tRow;
var
i : NativeInt;
Begin
i := length(s);
setlength(Result,length(s));
For i := i downto 0 do
result[i-1]:= ORD(s[i]=cChar[1]);
end;
 
function OutRow(const row:tRow):string;
//create output string
var
i: NativeInt;
Begin
i := length(row);
setlength(result,i);
For i := i downto 1 do
result[i]:= cChar[row[i-1]];
end;
 
procedure NextRow(row:pByteArray;MaxIdx:NativeInt);
//compute next row in place by the use of a small ringbuffer to memorize
//overridden elements
const
NextPos : array[0..2] of NativeInt = (1,2,0);
var
triple: Ttriple;
i,trpCnt,trActPos,trLastPos: NativeInt;
Begin
//fill the ring buffer
triple[0]:= 0;
i := row[0];
trpCnt := i;
triple[1] := i;
i := row[1];
inc(trpCnt,i);
triple[2] := i;
 
i := 0;
trActPos := 1;
trLastPos := 0;
while i < MaxIdx do
Begin
//the rule PopCnt = 2
row[i] := ORD(trpCnt= 2);
//reduce popcnt of element before
dec(trpCnt,triple[trLastPos]);
//position of next element
inc(i);
//replace last before element by next next element
triple[trLastPos]:= row[i+1];
//increment popcnt by next next element
inc(trpCnt,row[i+1]);
//move to next position in ring buffer
trLastPos := trActPos;
trActPos := NextPos[trActPos];
end;
row[MaxIdx] := ORD(trpCnt= 2);
end;
const
TestString: string=' ### ## # # # # # ';
var
s: string;
row:tRow;
i: NativeInt;
begin
s := Teststring;
row:= convert(s);
For i := 0 to 9 do
Begin
writeln(OutRow(row));
NextRow(@row[0],High(row));
end;
end.</lang>
{{Out}}<pre>
___###_##_#_#_#_#__#__
___#_#####_#_#_#______
____##___##_#_#_______
____##___###_#________
____##___#_##_________
____##____###_________
____##____#_#_________
____##_____#__________
____##________________
____##________________
</pre>
 
=={{header|Perl}}==
Line 3,498 ⟶ 3,605:
% the last four possibilies =>
% we consider that there is à 0 after the end
complang jq># The 1-d cellular automaton:
compute_next([0, 0], [0]).
def next:
# Conveniently, jq treats null as 0 when it comes to addition
# so there is no need to fiddle with the boundaries
. as $old
| reduce range(0; length) as $i
([];
($old[$i-1] + $old[$i+1]) as $s
| if $s == 0 then .[$i] = 0
elif $s == 1 then .[$i] = (if $old[$i] == 1 then 1 else 0 end)
else .[$i] = (if $old[$i] == 1 then 0 else 1 end)
end);
 
 
# pretty-print an array:
def pp: reduce .[] as $i (""; . + (if $i == 0 then " " else "*" end));
 
# continue until quiescence:
def go: recurse(. as $prev | next | if . == $prev then empty else . end) | pp;
 
# Example:
[0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0] | goute_next([0, 0], [0]).
 
compute_next([1, 0], [0]).
Line 4,445 ⟶ 4,573:
__##________________
</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
Anonymous user