One-dimensional cellular automata: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 19: | Line 19: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V gen = ‘_###_##_#_#_#_#__#__’.map(ch -> Int(ch == ‘#’)) |
||
L(n) 10 |
L(n) 10 |
||
print(gen.map(cell -> (I cell != 0 {‘#’} E ‘_’)).join(‘’)) |
print(gen.map(cell -> (I cell != 0 {‘#’} E ‘_’)).join(‘’)) |
||
gen = [0] [+] gen [+] [0] |
gen = [0] [+] gen [+] [0] |
||
gen = (0 .< gen.len - 2).map(m -> Int(sum(:gen[m .+ 3]) == 2))</ |
gen = (0 .< gen.len - 2).map(m -> Int(sum(:gen[m .+ 3]) == 2))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 40: | Line 40: | ||
=={{header|8th}}== |
=={{header|8th}}== |
||
< |
<syntaxhighlight lang="forth"> |
||
\ one-dimensional automaton |
\ one-dimensional automaton |
||
Line 73: | Line 73: | ||
bye |
bye |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ACL2}}== |
=={{header|ACL2}}== |
||
< |
<syntaxhighlight lang="lisp">(defun rc-step-r (cells) |
||
(if (endp (rest cells)) |
(if (endp (rest cells)) |
||
nil |
nil |
||
Line 108: | Line 108: | ||
nil |
nil |
||
(prog2$ (pretty-row (first out)) |
(prog2$ (pretty-row (first out)) |
||
(pretty-output (rest out)))))</ |
(pretty-output (rest out)))))</syntaxhighlight> |
||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">CHAR FUNC CalcCell(CHAR prev,curr,next) |
||
IF prev='. AND curr='# AND next='# THEN |
IF prev='. AND curr='# AND next='# THEN |
||
RETURN ('#) |
RETURN ('#) |
||
Line 148: | Line 148: | ||
FI |
FI |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/One-dimensional_cellular_automata.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/One-dimensional_cellular_automata.png Screenshot from Atari 8-bit computer] |
||
Line 165: | Line 165: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO; |
||
procedure Cellular_Automata is |
procedure Cellular_Automata is |
||
Line 206: | Line 206: | ||
Step (Culture); |
Step (Culture); |
||
end loop; |
end loop; |
||
end Cellular_Automata;</ |
end Cellular_Automata;</syntaxhighlight> |
||
The implementation defines Petri dish type with Boolean items |
The implementation defines Petri dish type with Boolean items |
||
Line 231: | Line 231: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
||
< |
<syntaxhighlight lang="algol68">INT stop generation = 9; |
||
INT universe width = 20; |
INT universe width = 20; |
||
FORMAT alive or dead = $b("#","_")$; |
FORMAT alive or dead = $b("#","_")$; |
||
Line 273: | Line 273: | ||
FI; |
FI; |
||
universe := next universe |
universe := next universe |
||
OD</ |
OD</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 293: | Line 293: | ||
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}} |
||
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}} |
||
< |
<syntaxhighlight lang="algol68">INT stop generation = 9; |
||
INT upb universe = 20; |
INT upb universe = 20; |
||
FORMAT alive or dead = $b("#","_")$; |
FORMAT alive or dead = $b("#","_")$; |
||
Line 327: | Line 327: | ||
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</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 344: | Line 344: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
Using a string to represent the cells and stopping when the next state is th same as the previous one. |
Using a string to represent the cells and stopping when the next state is th same as the previous one. |
||
< |
<syntaxhighlight lang="algolw">begin |
||
string(20) state; |
string(20) state; |
||
string(20) nextState; |
string(20) nextState; |
||
Line 363: | Line 363: | ||
generation := generation + 1 |
generation := generation + 1 |
||
end while_not_finished |
end while_not_finished |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 379: | Line 379: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">evolve: function [arr][ |
||
ary: [0] ++ arr ++ [0] |
ary: [0] ++ arr ++ [0] |
||
ret: new [] |
ret: new [] |
||
Line 405: | Line 405: | ||
newGen: evolve arr |
newGen: evolve arr |
||
printIt newGen |
printIt newGen |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 421: | Line 421: | ||
=={{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] |
||
< |
<syntaxhighlight lang="autohotkey">n := 22, n1 := n+1, v0 := v%n1% := 0 ; set grid dimensions, and fixed cells |
||
Loop % n { ; draw a line of checkboxes |
Loop % n { ; draw a line of checkboxes |
||
Line 443: | Line 443: | ||
GuiClose: ; exit when GUI is closed |
GuiClose: ; exit when GUI is closed |
||
ExitApp</ |
ExitApp</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang="awk">#!/usr/bin/awk -f |
||
BEGIN { |
BEGIN { |
||
edge = 1 |
edge = 1 |
||
Line 516: | Line 516: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 532: | Line 532: | ||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="awk">Another new solution (twice size as previous solution) : |
||
cat automata.awk : |
cat automata.awk : |
||
Line 575: | Line 575: | ||
} while (str_ != str_previous) |
} while (str_ != str_previous) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 596: | Line 596: | ||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="qbasic">DECLARE FUNCTION life$ (lastGen$) |
||
DECLARE FUNCTION getNeighbors! (group$) |
DECLARE FUNCTION getNeighbors! (group$) |
||
CLS |
CLS |
||
Line 648: | Line 648: | ||
NEXT i |
NEXT i |
||
life$ = newGen$ |
life$ = newGen$ |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Generation 0 : _###_##_#_#_#_#__#__ |
<pre>Generation 0 : _###_##_#_#_#_#__#__ |
||
Line 662: | Line 662: | ||
==={{header|FreeBASIC}}=== |
==={{header|FreeBASIC}}=== |
||
< |
<syntaxhighlight lang="freebasic">#define SIZE 640 |
||
randomize timer |
randomize timer |
||
Line 704: | Line 704: | ||
'or Q to exit |
'or Q to exit |
||
loop |
loop |
||
wend</ |
wend</syntaxhighlight> |
||
==={{header|Sinclair ZX81 BASIC}}=== |
==={{header|Sinclair ZX81 BASIC}}=== |
||
Works with the unexpanded (1k RAM) ZX81. |
Works with the unexpanded (1k RAM) ZX81. |
||
< |
<syntaxhighlight lang="basic"> 10 LET N$="01110110101010100100" |
||
20 LET G=1 |
20 LET G=1 |
||
30 PRINT N$ |
30 PRINT N$ |
||
Line 726: | Line 726: | ||
170 NEXT I |
170 NEXT I |
||
180 LET G=G+1 |
180 LET G=G+1 |
||
190 IF N$<>O$ THEN GOTO 40</ |
190 IF N$<>O$ THEN GOTO 40</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
The program overwrites each cell on the screen as it updates it (which it does quite slowly—there is no difficulty about watching what it is doing), with a counter to the right showing the generation it is currently working on. When it is part of the way through, for example, the display looks like this: |
The program overwrites each cell on the screen as it updates it (which it does quite slowly—there is no difficulty about watching what it is doing), with a counter to the right showing the generation it is currently working on. When it is part of the way through, for example, the display looks like this: |
||
Line 734: | Line 734: | ||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
< |
<syntaxhighlight lang="basic256">arraybase 1 |
||
dim start = {0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0} |
dim start = {0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0} |
||
dim sgtes(start[?]+1) |
dim sgtes(start[?]+1) |
||
Line 749: | Line 749: | ||
start[j] = sgtes[j] |
start[j] = sgtes[j] |
||
next j |
next j |
||
next k</ |
next k</syntaxhighlight> |
||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
This implementation will not stop showing generations, unless the cellular automata is already stable. |
This implementation will not stop showing generations, unless the cellular automata is already stable. |
||
< |
<syntaxhighlight lang="dos">@echo off |
||
setlocal enabledelayedexpansion |
setlocal enabledelayedexpansion |
||
Line 810: | Line 810: | ||
set proc=%newgen% |
set proc=%newgen% |
||
goto :nextgen |
goto :nextgen |
||
::/THE (LLLLLLOOOOOOOOOOOOONNNNNNNNGGGGGG.....) PROCESSOR</ |
::/THE (LLLLLLOOOOOOOOOOOOONNNNNNNNGGGGGG.....) PROCESSOR</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> | __###__##_#_##_###__######_###_#####_#__##_____#_#_#######__ | |
<pre> | __###__##_#_##_###__######_###_#####_#__##_____#_#_#######__ | |
||
Line 824: | Line 824: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> DIM rule$(7) |
||
rule$() = "0", "0", "0", "1", "0", "1", "1", "0" |
rule$() = "0", "0", "0", "1", "0", "1", "1", "0" |
||
Line 836: | Line 836: | ||
NEXT cell% |
NEXT cell% |
||
SWAP now$, next$ |
SWAP now$, next$ |
||
NEXT generation%</ |
NEXT generation%</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Generation 0: 01110110101010100100 |
<pre>Generation 0: 01110110101010100100 |
||
Line 850: | Line 850: | ||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
< |
<syntaxhighlight lang="befunge">v |
||
" !!! !! ! ! ! ! ! " ,*25 <v |
" !!! !! ! ! ! ! ! " ,*25 <v |
||
" " ,*25,,,,,,,,,,,,,,,,,,,,<v |
" " ,*25,,,,,,,,,,,,,,,,,,,,<v |
||
Line 865: | Line 865: | ||
^ >$$$$320p10g1+:9`v > >$"!"> 20g10g1+p 20g1+:20p |
^ >$$$$320p10g1+:9`v > >$"!"> 20g10g1+p 20g1+:20p |
||
^ v_10p10g |
^ v_10p10g |
||
> ^</ |
> ^</syntaxhighlight> |
||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat"> ( ( evolve |
||
= n z |
= n z |
||
. @( !arg |
. @( !arg |
||
Line 897: | Line 897: | ||
& evolve$!S:?S |
& evolve$!S:?S |
||
) |
) |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>11101101010101001001 |
<pre>11101101010101001001 |
||
Line 910: | Line 910: | ||
=={{header|C}}== |
=={{header|C}}== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 937: | Line 937: | ||
do { printf(c + 1); } while (evolve(c + 1, b + 1, sizeof(c) - 3)); |
do { printf(c + 1); } while (evolve(c + 1, b + 1, sizeof(c) - 3)); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>###_##_#_#_#_#__#__ |
<pre>###_##_#_#_#_#__#__ |
||
Line 950: | Line 950: | ||
Similar to above, but without a backup string: |
Similar to above, but without a backup string: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
char trans[] = "___#_##_"; |
char trans[] = "___#_##_"; |
||
Line 976: | Line 976: | ||
do { printf(c + 1); } while (evolve(c + 1, sizeof(c) - 3)); |
do { printf(c + 1); } while (evolve(c + 1, sizeof(c) - 3)); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp}}== |
=={{header|C sharp}}== |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 1,017: | Line 1,017: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Uses std::bitset for efficient packing of bit values. |
Uses std::bitset for efficient packing of bit values. |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <bitset> |
#include <bitset> |
||
#include <string> |
#include <string> |
||
Line 1,049: | Line 1,049: | ||
std::cout << std::endl; |
std::cout << std::endl; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,064: | Line 1,064: | ||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
< |
<syntaxhighlight lang="ceylon">shared abstract class Cell(character) of alive | dead { |
||
shared Character character; |
shared Character character; |
||
string => character.string; |
string => character.string; |
||
Line 1,129: | Line 1,129: | ||
print("generation `` ++generation `` ``automata``"); |
print("generation `` ++generation `` ``automata``"); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(ns one-dimensional-cellular-automata |
||
(:require (clojure.contrib (string :as s)))) |
(:require (clojure.contrib (string :as s)))) |
||
Line 1,147: | Line 1,147: | ||
'() |
'() |
||
(cons cells (generate (dec n) (next-gen cells))))) |
(cons cells (generate (dec n) (next-gen cells))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
< |
<syntaxhighlight lang="clojure">one-dimensional-cellular-automata> (doseq [cells (generate 9 "_###_##_#_#_#_#__#__")] |
||
(println cells)) |
(println cells)) |
||
_###_##_#_#_#_#__#__ |
_###_##_#_#_#_#__#__ |
||
Line 1,160: | Line 1,160: | ||
__##________________ |
__##________________ |
||
nil |
nil |
||
</syntaxhighlight> |
|||
</lang> |
|||
Another way: |
Another way: |
||
< |
<syntaxhighlight lang="clojure">#!/usr/bin/env lein-exec |
||
(require '[clojure.string :as str]) |
(require '[clojure.string :as str]) |
||
Line 1,188: | Line 1,188: | ||
(do (println g) |
(do (println g) |
||
(recur (compute-next-genr g) |
(recur (compute-next-genr g) |
||
(inc i)))))</ |
(inc i)))))</syntaxhighlight> |
||
Yet another way, easier to understand |
Yet another way, easier to understand |
||
< |
<syntaxhighlight lang="clojure"> |
||
(def rules |
(def rules |
||
{ |
{ |
||
Line 1,216: | Line 1,216: | ||
(doseq [g (take 10 (iterate nextgen [0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0]))] |
(doseq [g (take 10 (iterate nextgen [0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0]))] |
||
(println g)) |
(println g)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> |
||
Identification division. |
Identification division. |
||
Program-id. rc-1d-cell. |
Program-id. rc-1d-cell. |
||
Line 1,329: | Line 1,329: | ||
end-perform |
end-perform |
||
. |
. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,353: | Line 1,353: | ||
_##_____#__________ |
_##_____#__________ |
||
_##________________={{header|CoffeeScript}}== |
_##________________={{header|CoffeeScript}}== |
||
< |
<syntaxhighlight lang="coffeescript"> |
||
# We could cheat and count the bits, but let's keep this general. |
# We could cheat and count the bits, but let's keep this general. |
||
# . = dead, # = alive, middle cells survives iff one of the configurations |
# . = dead, # = alive, middle cells survives iff one of the configurations |
||
Line 1,386: | Line 1,386: | ||
simulate (c == '#' for c in ".###.##.#.#.#.#..#..") |
simulate (c == '#' for c in ".###.##.#.#.#.#..#..") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,404: | Line 1,404: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Based upon the Ruby version. |
Based upon the Ruby version. |
||
< |
<syntaxhighlight lang="lisp">(defun value (x) |
||
(assert (> (length x) 1)) |
(assert (> (length x) 1)) |
||
(coerce x 'simple-bit-vector)) |
(coerce x 'simple-bit-vector)) |
||
Line 1,431: | Line 1,431: | ||
do (princ (if (zerop (bit value i)) #\. #\#) |
do (princ (if (zerop (bit value i)) #\. #\#) |
||
stream)) |
stream)) |
||
(terpri stream))</ |
(terpri stream))</syntaxhighlight> |
||
< |
<syntaxhighlight lang="lisp">CL-USER> (loop for previous-value = nil then value |
||
for value = #*01110110101010100100 then (next-cycle value) |
for value = #*01110110101010100100 then (next-cycle value) |
||
until (equalp value previous-value) |
until (equalp value previous-value) |
||
Line 1,445: | Line 1,445: | ||
..##....#.#......... |
..##....#.#......... |
||
..##.....#.......... |
..##.....#.......... |
||
..##................</ |
..##................</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm; |
import std.stdio, std.algorithm; |
||
Line 1,466: | Line 1,466: | ||
writeln; |
writeln; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>__###_##_#_#_#_#__#___ |
<pre>__###_##_#_#_#_#__#___ |
||
Line 1,481: | Line 1,481: | ||
===Alternative Version=== |
===Alternative Version=== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.range; |
import std.stdio, std.algorithm, std.range; |
||
Line 1,493: | Line 1,493: | ||
A.swap(B); |
A.swap(B); |
||
} while (A != B); |
} while (A != B); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>_###_##_#_#_#_#__#__ |
<pre>_###_##_#_#_#_#__#__ |
||
Line 1,508: | Line 1,508: | ||
This version saves memory representing the state in an array of bits. For a higher performance a SWAR approach should be tried. |
This version saves memory representing the state in an array of bits. For a higher performance a SWAR approach should be tried. |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.range, std.bitmanip; |
import std.stdio, std.algorithm, std.range, std.bitmanip; |
||
Line 1,531: | Line 1,531: | ||
A.swap(B); |
A.swap(B); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
The output is the same as the second version. |
The output is the same as the second version. |
||
=={{header|DWScript}}== |
=={{header|DWScript}}== |
||
< |
<syntaxhighlight lang="delphi">const ngenerations = 10; |
||
const table = [0, 0, 0, 1, 0, 1, 1, 0]; |
const table = [0, 0, 0, 1, 0, 1, 1, 0]; |
||
Line 1,555: | Line 1,555: | ||
PrintLn(''); |
PrintLn(''); |
||
end; |
end; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>_###_##_#_#_#_#__#__ |
<pre>_###_##_#_#_#_#__#__ |
||
Line 1,570: | Line 1,570: | ||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
< |
<syntaxhighlight lang="dejavu">new-state size: |
||
0 ] |
0 ] |
||
repeat size: |
repeat size: |
||
Line 1,605: | Line 1,605: | ||
return print-state drop |
return print-state drop |
||
run 60</ |
run 60</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>001110011010110111001111110111011111010011000001010111111100 |
<pre>001110011010110111001111110111011111010011000001010111111100 |
||
Line 1,619: | Line 1,619: | ||
=={{header|E}}== |
=={{header|E}}== |
||
< |
<syntaxhighlight lang="e">def step(state, rule) { |
||
var result := state(0, 1) # fixed left cell |
var result := state(0, 1) # fixed left cell |
||
for i in 1..(state.size() - 2) { |
for i in 1..(state.size() - 2) { |
||
Line 1,636: | Line 1,636: | ||
} |
} |
||
return state |
return state |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="e">def rosettaRule := [ |
||
" " => " ", |
" " => " ", |
||
" #" => " ", |
" #" => " ", |
||
Line 1,660: | Line 1,660: | ||
8 | ## |
8 | ## |
||
9 | ## |
9 | ## |
||
# value: " ## "</ |
# value: " ## "</syntaxhighlight> |
||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
class |
class |
||
APPLICATION |
APPLICATION |
||
Line 1,736: | Line 1,736: | ||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,753: | Line 1,753: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def run(list, gen \\ 0) do |
def run(list, gen \\ 0) do |
||
print(list, gen) |
print(list, gen) |
||
Line 1,773: | Line 1,773: | ||
end |
end |
||
RC.run([0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0])</ |
RC.run([0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,790: | Line 1,790: | ||
=={{header|Elm}}== |
=={{header|Elm}}== |
||
< |
<syntaxhighlight lang="elm">import Maybe exposing (withDefault) |
||
import List exposing (length, tail, reverse, concat, head, append, map3) |
import List exposing (length, tail, reverse, concat, head, append, map3) |
||
import Html exposing (Html, div, h1, text) |
import Html exposing (Html, div, h1, text) |
||
Line 1,879: | Line 1,879: | ||
, update = update |
, update = update |
||
, subscriptions = subscriptions |
, subscriptions = subscriptions |
||
}</ |
}</syntaxhighlight> |
||
Link to live demo: https://dc25.github.io/oneDimensionalCellularAutomataElm/ |
Link to live demo: https://dc25.github.io/oneDimensionalCellularAutomataElm/ |
||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang"> |
||
-module(ca). |
-module(ca). |
||
-compile(export_all). |
-compile(export_all). |
||
Line 1,933: | Line 1,933: | ||
next([1,1,1|T],Acc) -> |
next([1,1,1|T],Acc) -> |
||
next([1,1|T],[0|Acc]). |
next([1,1|T],[0|Acc]). |
||
</syntaxhighlight> |
|||
</lang> |
|||
Example execution: |
Example execution: |
||
< |
<syntaxhighlight lang="erlang"> |
||
44> ca:run(9,[0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0]). |
44> ca:run(9,[0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0]). |
||
0: __###_##_#_#_#_#__#___ |
0: __###_##_#_#_#_#__#___ |
||
Line 1,947: | Line 1,947: | ||
8: ___##_________________ |
8: ___##_________________ |
||
9: ___##_________________ |
9: ___##_________________ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
<syntaxhighlight lang="erre"> |
|||
<lang ERRE> |
|||
PROGRAM ONEDIM_AUTOMATA |
PROGRAM ONEDIM_AUTOMATA |
||
Line 1,984: | Line 1,984: | ||
END FOR |
END FOR |
||
END PROGRAM |
END PROGRAM |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,010: | Line 2,010: | ||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
< |
<syntaxhighlight lang="euphoria">include machine.e |
||
function rules(integer tri) |
function rules(integer tri) |
||
Line 2,055: | Line 2,055: | ||
printf(1,"Generation %d: ",n) |
printf(1,"Generation %d: ",n) |
||
print_gen(gen)</ |
print_gen(gen)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,069: | Line 2,069: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: bit-arrays io kernel locals math sequences ; |
||
IN: cellular |
IN: cellular |
||
Line 2,090: | Line 2,090: | ||
10 [ dup print-cellular step ] times print-cellular ; |
10 [ dup print-cellular step ] times print-cellular ; |
||
MAIN: main-cellular |
MAIN: main-cellular |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre>( scratchpad ) "cellular" run |
<pre>( scratchpad ) "cellular" run |
||
_###_##_#_#_#_#__#__ |
_###_##_#_#_#_#__#__ |
||
Line 2,105: | Line 2,105: | ||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Automaton |
class Automaton |
||
{ |
{ |
||
Line 2,136: | Line 2,136: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FOCAL}}== |
=={{header|FOCAL}}== |
||
< |
<syntaxhighlight lang="focal">1.1 S OLD(2)=1; S OLD(3)=1; S OLD(4)=1; S OLD(6)=1; S OLD(7)=1 |
||
1.2 S OLD(9)=1; S OLD(11)=1; S OLD(13)=1; S OLD(15)=1; S OLD(18)=1 |
1.2 S OLD(9)=1; S OLD(11)=1; S OLD(13)=1; S OLD(15)=1; S OLD(18)=1 |
||
1.3 F N=1,10; D 2 |
1.3 F N=1,10; D 2 |
||
Line 2,159: | Line 2,159: | ||
7.1 T "#" |
7.1 T "#" |
||
8.1 T "."</ |
8.1 T "."</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
<pre> |
<pre> |
||
Line 2,175: | Line 2,175: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: init ( bits count -- ) |
||
0 do dup 1 and c, 2/ loop drop ; |
0 do dup 1 and c, 2/ loop drop ; |
||
Line 2,199: | Line 2,199: | ||
.state 1 do gen .state loop ; |
.state 1 do gen .state loop ; |
||
10 life1d</ |
10 life1d</syntaxhighlight> |
||
ouput |
ouput |
||
<lang> |
<syntaxhighlight lang="text"> |
||
### ## # # # # # |
### ## # # # # # |
||
# ##### # # # |
# ##### # # # |
||
Line 2,213: | Line 2,213: | ||
## |
## |
||
## ok |
## ok |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{works with|Fortran|90 and later}} |
{{works with|Fortran|90 and later}} |
||
< |
<syntaxhighlight lang="fortran">PROGRAM LIFE_1D |
||
IMPLICIT NONE |
IMPLICIT NONE |
||
Line 2,266: | Line 2,266: | ||
END SUBROUTINE Drawgen |
END SUBROUTINE Drawgen |
||
END PROGRAM LIFE_1D</ |
END PROGRAM LIFE_1D</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,282: | Line 2,282: | ||
=={{header|GFA Basic}}== |
=={{header|GFA Basic}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
' |
' |
||
' One Dimensional Cellular Automaton |
' One Dimensional Cellular Automaton |
||
Line 2,396: | Line 2,396: | ||
RETURN result% |
RETURN result% |
||
ENDFUNC |
ENDFUNC |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Sequential=== |
===Sequential=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,437: | Line 2,437: | ||
return g0[1:last] |
return g0[1:last] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,457: | Line 2,457: | ||
Separate read and write phases. |
Separate read and write phases. |
||
Single array of cells. |
Single array of cells. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 2,511: | Line 2,511: | ||
write.Wait() |
write.Wait() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output is same as sequential version. |
Output is same as sequential version. |
||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">def life1D = { self -> |
||
def right = self[1..-1] + [false] |
def right = self[1..-1] + [false] |
||
def left = [false] + self[0..-2] |
def left = [false] + self[0..-2] |
||
[left, self, right].transpose().collect { hood -> hood.count { it } == 2 } |
[left, self, right].transpose().collect { hood -> hood.count { it } == 2 } |
||
}</ |
}</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">def cells = ('_###_##_#_#_#_#__#__' as List).collect { it == '#' } |
||
println "Generation 0: ${cells.collect { g -> g ? '#' : '_' }.join()}" |
println "Generation 0: ${cells.collect { g -> g ? '#' : '_' }.join()}" |
||
(1..9).each { |
(1..9).each { |
||
cells = life1D(cells) |
cells = life1D(cells) |
||
println "Generation ${it}: ${cells.collect { g -> g ? '#' : '_' }.join()}" |
println "Generation ${it}: ${cells.collect { g -> g ? '#' : '_' }.join()}" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,543: | Line 2,543: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (unfoldr) |
||
import System.Random (newStdGen, randomRs) |
import System.Random (newStdGen, randomRs) |
||
Line 2,573: | Line 2,573: | ||
. take 36 |
. take 36 |
||
. randomRs (0, 1) |
. randomRs (0, 1) |
||
)</ |
)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
For example: |
For example: |
||
Line 2,587: | Line 2,587: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon"> |
||
# One dimensional Cellular automaton |
# One dimensional Cellular automaton |
||
record Automaton(size, cells) |
record Automaton(size, cells) |
||
Line 2,624: | Line 2,624: | ||
} |
} |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
An alternative approach is to represent the automaton as a string. |
An alternative approach is to represent the automaton as a string. |
||
Line 2,635: | Line 2,635: | ||
the evolve procedure fails if a new generation is unchanged from the previous, stopping the generation cycle early. |
the evolve procedure fails if a new generation is unchanged from the previous, stopping the generation cycle early. |
||
< |
<syntaxhighlight lang="unicon">procedure main(A) |
||
A := if *A = 0 then ["01110110101010100100"] |
A := if *A = 0 then ["01110110101010100100"] |
||
CA := show("0"||A[1]||"0") # add always dead border cells |
CA := show("0"||A[1]||"0") # add always dead border cells |
||
Line 2,650: | Line 2,650: | ||
every newCA[i := 2 to (*CA-1)] := (CA[i-1]+CA[i]+CA[i+1] = 2, "1") |
every newCA[i := 2 to (*CA-1)] := (CA[i-1]+CA[i]+CA[i+1] = 2, "1") |
||
return CA ~== newCA # fail if no change |
return CA ~== newCA # fail if no change |
||
end</ |
end</syntaxhighlight> |
||
{{out|A couple of sample runs}} |
{{out|A couple of sample runs}} |
||
Line 2,671: | Line 2,671: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">life1d=: '_#'{~ (2 = 3+/\ 0,],0:)^:a:</syntaxhighlight> |
||
{{out|Example use}} |
{{out|Example use}} |
||
< |
<syntaxhighlight lang="j"> life1d ? 20 # 2 |
||
_###_##_#_#_#_#__#__ |
_###_##_#_#_#_#__#__ |
||
_#_#####_#_#_#______ |
_#_#####_#_#_#______ |
||
Line 2,683: | Line 2,683: | ||
__##____#_#_________ |
__##____#_#_________ |
||
__##_____#__________ |
__##_____#__________ |
||
__##________________</ |
__##________________</syntaxhighlight> |
||
Alternative implementation: |
Alternative implementation: |
||
< |
<syntaxhighlight lang="j">Rule=:2 :0 NB. , m: number of generations, n: rule number |
||
'_#'{~ (3 ((|.n#:~8#2) {~ #.)\ 0,],0:)^:(i.m) |
'_#'{~ (3 ((|.n#:~8#2) {~ #.)\ 0,],0:)^:(i.m) |
||
)</ |
)</syntaxhighlight> |
||
{{out|Example use}} |
{{out|Example use}} |
||
< |
<syntaxhighlight lang="j"> 9 Rule 104 '#'='_###_##_#_#_#_#__#__' |
||
_###_##_#_#_#_#__#__ |
_###_##_#_#_#_#__#__ |
||
_#_#####_#_#_#______ |
_#_#####_#_#_#______ |
||
Line 2,701: | Line 2,701: | ||
__##____#_#_________ |
__##____#_#_________ |
||
__##_____#__________ |
__##_____#__________ |
||
__##________________</ |
__##________________</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
This example requires a starting generation of at least length two |
This example requires a starting generation of at least length two |
||
(which is what you need for anything interesting anyway). |
(which is what you need for anything interesting anyway). |
||
< |
<syntaxhighlight lang="java">public class Life{ |
||
public static void main(String[] args) throws Exception{ |
public static void main(String[] args) throws Exception{ |
||
String start= "_###_##_#_#_#_#__#__"; |
String start= "_###_##_#_#_#_#__#__"; |
||
Line 2,747: | Line 2,747: | ||
return ans; |
return ans; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Generation 0: _###_##_#_#_#_#__#__ |
<pre>Generation 0: _###_##_#_#_#_#__#__ |
||
Line 2,763: | Line 2,763: | ||
which is local to the <code>evolve</code> method, |
which is local to the <code>evolve</code> method, |
||
and the <code>evolve</code> method returns a boolean. |
and the <code>evolve</code> method returns a boolean. |
||
< |
<syntaxhighlight lang="java">public class Life{ |
||
private static char[] trans = "___#_##_".toCharArray(); |
private static char[] trans = "___#_##_".toCharArray(); |
||
Line 2,793: | Line 2,793: | ||
}while(evolve(c)); |
}while(evolve(c)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>###_##_#_#_#_#__#__ |
<pre>###_##_#_#_#_#__#__ |
||
Line 2,812: | Line 2,812: | ||
state[i-1] refers to the new cell in question, |
state[i-1] refers to the new cell in question, |
||
(old[i] == 1) checks if the old cell was alive. |
(old[i] == 1) checks if the old cell was alive. |
||
< |
<syntaxhighlight lang="javascript">function caStep(old) { |
||
var old = [0].concat(old, [0]); // Surround with dead cells. |
var old = [0].concat(old, [0]); // Surround with dead cells. |
||
var state = []; // The new state. |
var state = []; // The new state. |
||
Line 2,824: | Line 2,824: | ||
} |
} |
||
return state; |
return state; |
||
}</ |
}</syntaxhighlight> |
||
{{out|Example usage}} |
{{out|Example usage}} |
||
< |
<syntaxhighlight lang="javascript">alert(caStep([0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]));</syntaxhighlight> |
||
shows an alert with "0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,0,0,0,0,0". |
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}}== |
=={{header|jq}}== |
||
The main point of interest in the following is perhaps the way the built-in function "recurse" is used to continue the simulation until quiescence. |
The main point of interest in the following is perhaps the way the built-in function "recurse" is used to continue the simulation until quiescence. |
||
< |
<syntaxhighlight lang="jq"># The 1-d cellular automaton: |
||
def next: |
def next: |
||
# Conveniently, jq treats null as 0 when it comes to addition |
# Conveniently, jq treats null as 0 when it comes to addition |
||
Line 2,853: | Line 2,853: | ||
# Example: |
# Example: |
||
[0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0] | go</ |
[0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0] | go</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -c -r -n -f One-dimensional_cellular_automata.jq |
||
*** ** * * * * * |
*** ** * * * * * |
||
* ***** * * * |
* ***** * * * |
||
Line 2,864: | Line 2,864: | ||
** * * |
** * * |
||
** * |
** * |
||
**</ |
**</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
This solution creates an automaton with with either empty or periodic bounds. The empty bounds case, is typical of many of the solutions here. The periodic bounds case is a typical physics approach where, in effect, the beginning and end of the list touch each other to form a circular rather than linear array. In practice, the effects of boundary conditions are subtle for long arrays. |
This solution creates an automaton with with either empty or periodic bounds. The empty bounds case, is typical of many of the solutions here. The periodic bounds case is a typical physics approach where, in effect, the beginning and end of the list touch each other to form a circular rather than linear array. In practice, the effects of boundary conditions are subtle for long arrays. |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
function next_gen(a::BitArray{1}, isperiodic=false) |
function next_gen(a::BitArray{1}, isperiodic=false) |
||
b = copy(a) |
b = copy(a) |
||
Line 2,904: | Line 2,904: | ||
a = next_gen(a, true) |
a = next_gen(a, true) |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,927: | Line 2,927: | ||
=={{header|K}}== |
=={{header|K}}== |
||
< |
<syntaxhighlight lang="k">f:{2=+/(0,x,0)@(!#x)+/:!3}</syntaxhighlight> |
||
{{out|Example usage}} |
{{out|Example usage}} |
||
< |
<syntaxhighlight lang="k"> `0:"_X"@f\0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0 |
||
_XXX_XX_X_X_X_X__X__ |
_XXX_XX_X_X_X_X__X__ |
||
_X_XXXXX_X_X_X______ |
_X_XXXXX_X_X_X______ |
||
Line 2,940: | Line 2,940: | ||
__XX_____X__________ |
__XX_____X__________ |
||
__XX________________ |
__XX________________ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.4-3 |
||
val trans = "___#_##_" |
val trans = "___#_##_" |
||
Line 2,970: | Line 2,970: | ||
} |
} |
||
while (evolve(c,b)) |
while (evolve(c,b)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,988: | Line 2,988: | ||
{{works with|Just BASIC}} |
{{works with|Just BASIC}} |
||
{{works with|Run BASIC}} |
{{works with|Run BASIC}} |
||
< |
<syntaxhighlight lang="lb">' [RC] 'One-dimensional cellular automata' |
||
' does not wrap so fails for some rules |
' does not wrap so fails for some rules |
||
Line 3,012: | Line 3,012: | ||
next j |
next j |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Locomotive Basic}}== |
=={{header|Locomotive Basic}}== |
||
< |
<syntaxhighlight lang="locobasic">10 MODE 1:n=10:READ w:DIM x(w+1),x2(w+1):FOR i=1 to w:READ x(i):NEXT |
||
20 FOR k=1 TO n |
20 FOR k=1 TO n |
||
30 FOR j=1 TO w |
30 FOR j=1 TO w |
||
Line 3,024: | Line 3,024: | ||
70 FOR j=1 TO w:x(j)=x2(j):NEXT |
70 FOR j=1 TO w:x(j)=x2(j):NEXT |
||
80 NEXT |
80 NEXT |
||
90 DATA 20,0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0</ |
90 DATA 20,0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,031: | Line 3,031: | ||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
{{works with|UCB Logo}} |
{{works with|UCB Logo}} |
||
< |
<syntaxhighlight 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 3,068: | Line 3,068: | ||
end |
end |
||
CA_1D :cell_list :generations</ |
CA_1D :cell_list :generations</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,084: | Line 3,084: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">num_iterations = 9 |
||
f = { 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 } |
f = { 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0 } |
||
Line 3,115: | Line 3,115: | ||
Output( f, l ) |
Output( f, l ) |
||
end </ |
end </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0: _###_##_#_#_#_#__#__ |
<pre>0: _###_##_#_#_#_#__#__ |
||
Line 3,129: | Line 3,129: | ||
=={{header|M4}}== |
=={{header|M4}}== |
||
< |
<syntaxhighlight lang="m4">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 3,164: | Line 3,164: | ||
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)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,183: | Line 3,183: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Built-in function: |
Built-in function: |
||
< |
<syntaxhighlight 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] |
||
Print @@@ (% /. {1 -> "#", 0 -> "."});</ |
Print @@@ (% /. {1 -> "#", 0 -> "."});</syntaxhighlight> |
||
For succinctness, an integral rule can be used: |
For succinctness, an integral rule can be used: |
||
< |
<syntaxhighlight lang="mathematica">CellularAutomaton[2^^01101000 (* == 104 *), {{1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1}, 0}, 12];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="mathematica">###.##.#.#.#.#..# |
||
#.#####.#.#.#.... |
#.#####.#.#.#.... |
||
.##...##.#.#..... |
.##...##.#.#..... |
||
Line 3,200: | Line 3,200: | ||
.##.............. |
.##.............. |
||
.##.............. |
.##.............. |
||
.##..............</ |
.##..............</syntaxhighlight> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">function one_dim_cell_automata(v,n) |
||
V='_#'; |
V='_#'; |
||
while n>=0; |
while n>=0; |
||
Line 3,211: | Line 3,211: | ||
v = v(3:end)==2; |
v = v(3:end)==2; |
||
end; |
end; |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>octave:27> one_dim_cell_automata('01110110101010100100'=='1',20); |
<pre>octave:27> one_dim_cell_automata('01110110101010100100'=='1',20); |
||
Line 3,233: | Line 3,233: | ||
but it segfaults when trying to use <code>BOOLEAN</code> types, |
but it segfaults when trying to use <code>BOOLEAN</code> types, |
||
so we use <code>INTEGER</code> instead. |
so we use <code>INTEGER</code> instead. |
||
< |
<syntaxhighlight lang="modula3">MODULE Cell EXPORTS Main; |
||
IMPORT IO, Fmt, Word; |
IMPORT IO, Fmt, Word; |
||
Line 3,275: | Line 3,275: | ||
Step(culture); |
Step(culture); |
||
END; |
END; |
||
END Cell.</ |
END Cell.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,291: | Line 3,291: | ||
=={{header|MontiLang}}== |
=={{header|MontiLang}}== |
||
< |
<syntaxhighlight lang="montilang">30 VAR length . |
||
35 VAR height . |
35 VAR height . |
||
FOR length 0 ENDFOR 1 0 ARR VAR list . |
FOR length 0 ENDFOR 1 0 ARR VAR list . |
||
Line 3,369: | Line 3,369: | ||
next printArr . |
next printArr . |
||
next 0 ADD APPEND . VAR list . |
next 0 ADD APPEND . VAR list . |
||
ENDFOR</ |
ENDFOR</syntaxhighlight> |
||
=={{header|Nial}}== |
=={{header|Nial}}== |
||
(life.nial) |
(life.nial) |
||
< |
<syntaxhighlight 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 |
||
Line 3,382: | Line 3,382: | ||
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 ]</syntaxhighlight> |
||
{{out|Using it}} |
{{out|Using it}} |
||
< |
<syntaxhighlight 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</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import random |
||
Line 3,437: | Line 3,437: | ||
if map2 == map: break |
if map2 == map: break |
||
map = map2</ |
map = map2</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>_###_##_#_#_#_#__#____________ |
<pre>_###_##_#_#_#_#__#____________ |
||
Line 3,451: | Line 3,451: | ||
'''Using a string character counting method''': |
'''Using a string character counting method''': |
||
< |
<syntaxhighlight lang="nim">import strutils |
||
const |
const |
||
Line 3,478: | Line 3,478: | ||
q0 = q1 |
q0 = q1 |
||
q1 = evolve(q0) |
q1 = evolve(q0) |
||
echo(q1)</ |
echo(q1)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>_###_##_#_#_#_#__#__ |
<pre>_###_##_#_#_#_#__#__ |
||
Line 3,492: | Line 3,492: | ||
'''Using nested functions and method calling style:''' |
'''Using nested functions and method calling style:''' |
||
< |
<syntaxhighlight lang="nim">proc cellAutomata = |
||
proc evolveInto(x, t : var string) = |
proc evolveInto(x, t : var string) = |
||
for i in x.low..x.high: |
for i in x.low..x.high: |
||
Line 3,512: | Line 3,512: | ||
swap t, x |
swap t, x |
||
cellAutomata()</ |
cellAutomata()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,528: | Line 3,528: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let get g i = |
||
try g.(i) |
try g.(i) |
||
with _ -> 0 |
with _ -> 0 |
||
Line 3,556: | Line 3,556: | ||
else print_char '#' |
else print_char '#' |
||
done; |
done; |
||
print_newline()</ |
print_newline()</syntaxhighlight> |
||
put the code above in a file named "life.ml", |
put the code above in a file named "life.ml", |
||
Line 3,592: | Line 3,592: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: nextGen( l ) |
||
| i s | |
| i s | |
||
l byteSize dup ->s String newSize |
l byteSize dup ->s String newSize |
||
Line 3,604: | Line 3,604: | ||
: gen( l n -- ) |
: gen( l n -- ) |
||
l dup .cr #[ nextGen dup .cr ] times( n ) drop ;</ |
l dup .cr #[ nextGen dup .cr ] times( n ) drop ;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,624: | Line 3,624: | ||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">declare |
||
A0 = {List.toTuple unit "_###_##_#_#_#_#__#__"} |
A0 = {List.toTuple unit "_###_##_#_#_#_#__#__"} |
||
Line 3,659: | Line 3,659: | ||
do |
do |
||
{System.showInfo "Gen. "#I#": "#{Record.toList A}} |
{System.showInfo "Gen. "#I#": "#{Record.toList A}} |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,679: | Line 3,679: | ||
This function generates one generation from a previous one, |
This function generates one generation from a previous one, |
||
passed as a 0-1 vector. |
passed as a 0-1 vector. |
||
< |
<syntaxhighlight 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;</syntaxhighlight> |
||
To simulate a run of 10 generations of the automaton, the function above can be put in a loop that spawns a new generation as a function of nth generations passed (n=0 is the initial state): |
To simulate a run of 10 generations of the automaton, the function above can be put in a loop that spawns a new generation as a function of nth generations passed (n=0 is the initial state): |
||
< |
<syntaxhighlight lang="parigp">cur = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0]; for(n=0, 9, print(cur); cur = step(cur));</syntaxhighlight> |
||
=== Output === |
=== Output === |
||
<lang> |
<syntaxhighlight lang="text"> |
||
[0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0] |
[0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0] |
||
[0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0] |
[0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0] |
||
Line 3,697: | Line 3,697: | ||
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
||
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">program Test; |
||
{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE}{$ENDIF} |
{$IFDEF FPC}{$MODE DELPHI}{$ELSE}{$APPTYPE}{$ENDIF} |
||
uses |
uses |
||
Line 3,772: | Line 3,772: | ||
NextRow(@row[0],High(row)); |
NextRow(@row[0],High(row)); |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
{{Out}}<pre> |
{{Out}}<pre> |
||
__###_##_#_#_#_#__#__ |
__###_##_#_#_#_#__#__ |
||
Line 3,790: | Line 3,790: | ||
Convert cells to zeros and ones to set complement state |
Convert cells to zeros and ones to set complement state |
||
< |
<syntaxhighlight lang="perl"> |
||
$_="_###_##_#_#_#_#__#__\n"; |
$_="_###_##_#_#_#_#__#__\n"; |
||
do { |
do { |
||
Line 3,798: | Line 3,798: | ||
s/(?<=(.))(.)(?=(.))/$1 == $3 ? $1 ? 1-$2 : 0 : $2/eg; |
s/(?<=(.))(.)(?=(.))/$1 == $3 ? $1 ? 1-$2 : 0 : $2/eg; |
||
} while ($x ne $_ and $x=$_); |
} while ($x ne $_ and $x=$_); |
||
</syntaxhighlight> |
|||
</lang> |
|||
Use hash for complement state |
Use hash for complement state |
||
< |
<syntaxhighlight lang="perl"> |
||
$_="_###_##_#_#_#_#__#__\n"; |
$_="_###_##_#_#_#_#__#__\n"; |
||
%h=qw(# _ _ #); |
%h=qw(# _ _ #); |
||
Line 3,808: | Line 3,808: | ||
s/(?<=(.))(.)(?=(.))/$1 eq $3 ? $1 eq "_" ? "_" : $h{$2} : $2/eg; |
s/(?<=(.))(.)(?=(.))/$1 eq $3 ? $1 eq "_" ? "_" : $h{$2} : $2/eg; |
||
} while ($x ne $_ and $x=$_); |
} while ($x ne $_ and $x=$_); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} for both versions: |
{{out}} for both versions: |
||
Line 3,823: | Line 3,823: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Ludicrously optimised: |
Ludicrously optimised: |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"_###_##_#_#_#_#__#__"</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"_###_##_#_#_#_#__#__"</span> |
||
<span style="color: #004080;">integer</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'_'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">toggled</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
<span style="color: #004080;">integer</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'_'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">toggled</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
||
Line 3,841: | Line 3,841: | ||
<span style="color: #000000;">toggled</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
<span style="color: #000000;">toggled</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre style="font-size: 8px"> |
<pre style="font-size: 8px"> |
||
Line 3,856: | Line 3,856: | ||
</pre> |
</pre> |
||
And of course I had to have a crack at that Sierpinski_Triangle: |
And of course I had to have a crack at that Sierpinski_Triangle: |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"________________________#________________________"</span> |
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"________________________#________________________"</span> |
||
<span style="color: #004080;">integer</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'_'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">toggled</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
<span style="color: #004080;">integer</span> <span style="color: #000000;">prev</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'_'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">toggled</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> |
||
Line 3,870: | Line 3,870: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre style="font-size: 6px"> |
<pre style="font-size: 6px"> |
||
Line 3,900: | Line 3,900: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang="phixmonti">0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0 stklen var w |
||
w tolist 0 0 put |
w tolist 0 0 put |
||
0 w 1 + repeat var x2 |
0 w 1 + repeat var x2 |
||
Line 3,914: | Line 3,914: | ||
nl |
nl |
||
drop x2 |
drop x2 |
||
endfor</ |
endfor</syntaxhighlight> |
||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">go => |
||
% _ # # # _ # # _ # _ # _ # _ # _ _ # _ _ |
% _ # # # _ # # _ # _ # _ # _ # _ _ # _ _ |
||
S = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0], |
S = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0], |
||
Line 3,967: | Line 3,967: | ||
rule([1,0,1]) = 1. % Two neighbours giving birth |
rule([1,0,1]) = 1. % Two neighbours giving birth |
||
rule([1,1,0]) = 1. % Needs one neighbour to survive |
rule([1,1,0]) = 1. % Needs one neighbour to survive |
||
rule([1,1,1]) = 0. % Starved to death.</ |
rule([1,1,1]) = 0. % Starved to death.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,015: | Line 4,015: | ||
The program is fairly general. Here's the additional code for the rule 30 CA. |
The program is fairly general. Here's the additional code for the rule 30 CA. |
||
< |
<syntaxhighlight lang="picat">go2 => |
||
N = 4, |
N = 4, |
||
Ns = [0 : _ in 1..N], |
Ns = [0 : _ in 1..N], |
||
Line 4,029: | Line 4,029: | ||
rule30([1,0,1]) = 0. |
rule30([1,0,1]) = 0. |
||
rule30([1,1,0]) = 0. |
rule30([1,1,0]) = 0. |
||
rule30([1,1,1]) = 0.</ |
rule30([1,1,1]) = 0.</syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(let Cells (chop "_###_##_#_#_#_#__#__") |
||
(do 10 |
(do 10 |
||
(prinl Cells) |
(prinl Cells) |
||
Line 4,046: | Line 4,046: | ||
(link "#") ) ) ) |
(link "#") ) ) ) |
||
Cells ) |
Cells ) |
||
(link "_") ) ) ) )</ |
(link "_") ) ) ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>_###_##_#_#_#_#__#__ |
<pre>_###_##_#_#_#_#__#__ |
||
Line 4,061: | Line 4,061: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
Works ith SWI-Prolog. |
Works ith SWI-Prolog. |
||
< |
<syntaxhighlight lang="prolog">one_dimensional_cellular_automata(L) :- |
||
maplist(my_write, L), nl, |
maplist(my_write, L), nl, |
||
length(L, N), |
length(L, N), |
||
Line 4,134: | Line 4,134: | ||
L = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0], |
L = [0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0], |
||
one_dimensional_cellular_automata(L). |
one_dimensional_cellular_automata(L). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> ?- one_dimensional_cellular_automata. |
<pre> ?- one_dimensional_cellular_automata. |
||
Line 4,150: | Line 4,150: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">EnableExplicit |
||
Dim cG.i(21) |
Dim cG.i(21) |
||
Dim nG.i(21) |
Dim nG.i(21) |
||
Line 4,180: | Line 4,180: | ||
Until Gen > 9 |
Until Gen > 9 |
||
PrintN("Press any key to exit"): Repeat: Until Inkey() <> ""</ |
PrintN("Press any key to exit"): Repeat: Until Inkey() <> ""</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Generation 0: _###_##_#_#_#_#__#__ |
<pre>Generation 0: _###_##_#_#_#_#__#__ |
||
Line 4,196: | Line 4,196: | ||
===Procedural=== |
===Procedural=== |
||
====Python: Straightforward interpretation of spec==== |
====Python: Straightforward interpretation of spec==== |
||
< |
<syntaxhighlight lang="python">import random |
||
printdead, printlive = '_#' |
printdead, printlive = '_#' |
||
Line 4,220: | Line 4,220: | ||
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))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Generation 0: _###_##_#_#_#_#__#__ |
<pre>Generation 0: _###_##_#_#_#_#__#__ |
||
Line 4,235: | Line 4,235: | ||
====Python: Using boolean operators on bits==== |
====Python: Using boolean operators on bits==== |
||
The following implementation uses boolean operations to realize the function. |
The following implementation uses boolean operations to realize the function. |
||
< |
<syntaxhighlight lang="python">import random |
||
nquads = 5 |
nquads = 5 |
||
Line 4,250: | Line 4,250: | ||
print "Generation %3i: %s" % (i,(''.join(tr[int(t,16)] for t in (fmt%(a>>1))))) |
print "Generation %3i: %s" % (i,(''.join(tr[int(t,16)] for t in (fmt%(a>>1))))) |
||
a |= endvals |
a |= endvals |
||
a = ((a&((a<<1) | (a>>1))) ^ ((a<<1)&(a>>1))) & endmask</ |
a = ((a&((a<<1) | (a>>1))) ^ ((a<<1)&(a>>1))) & endmask</syntaxhighlight> |
||
====Python: Sum neighbours == 2==== |
====Python: Sum neighbours == 2==== |
||
This example makes use of the observation that a cell is alive in the next generation if the sum with its current neighbours of alive cells is two. |
This example makes use of the observation that a cell is alive in the next generation if the sum with its current neighbours of alive cells is two. |
||
< |
<syntaxhighlight lang="python">>>> gen = [ch == '#' for ch in '_###_##_#_#_#_#__#__'] |
||
>>> for n in range(10): |
>>> for n in range(10): |
||
print(''.join('#' if cell else '_' for cell in gen)) |
print(''.join('#' if cell else '_' for cell in gen)) |
||
Line 4,271: | Line 4,271: | ||
__##________________ |
__##________________ |
||
__##________________ |
__##________________ |
||
>>> </ |
>>> </syntaxhighlight> |
||
===Composition of pure functions=== |
===Composition of pure functions=== |
||
Interpreting the rule shown in the task description as Wolfram rule 104, and generalising enough to allow for other rules of this kind: |
Interpreting the rule shown in the task description as Wolfram rule 104, and generalising enough to allow for other rules of this kind: |
||
< |
<syntaxhighlight lang="python">'''Cellular Automata''' |
||
from itertools import islice, repeat |
from itertools import islice, repeat |
||
Line 4,481: | Line 4,481: | ||
# MAIN ------------------------------------------------- |
# MAIN ------------------------------------------------- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Rule 104: |
<pre>Rule 104: |
||
Line 4,537: | Line 4,537: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ stack 0 ] is cells ( --> s ) |
||
[ dup size cells replace |
[ dup size cells replace |
||
Line 4,564: | Line 4,564: | ||
echoline ] is automate ( $ --> ) |
echoline ] is automate ( $ --> ) |
||
$ "_###_##_#_#_#_#__#__" automate</ |
$ "_###_##_#_#_#_#__#__" automate</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,583: | Line 4,583: | ||
=={{header|R}}== |
=={{header|R}}== |
||
< |
<syntaxhighlight lang="r">set.seed(15797, kind="Mersenne-Twister") |
||
maxgenerations = 10 |
maxgenerations = 10 |
||
Line 4,615: | Line 4,615: | ||
universe <- cellularAutomata(universe, stayingAlive) |
universe <- cellularAutomata(universe, stayingAlive) |
||
cat(format(i, width=3), deadOrAlive2string(universe), "\n") |
cat(format(i, width=3), deadOrAlive2string(universe), "\n") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,632: | Line 4,632: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (update cells) |
(define (update cells) |
||
Line 4,658: | Line 4,658: | ||
(0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0) |
(0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0) |
||
(0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
(0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |
||
(0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |#</ |
(0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |#</syntaxhighlight> |
||
Below is an alternative implementation using graphical output in the Racket REPL. |
Below is an alternative implementation using graphical output in the Racket REPL. |
||
It works with DrRacket and Emacs + Geiser. |
It works with DrRacket and Emacs + Geiser. |
||
< |
<syntaxhighlight lang="racket">#lang slideshow |
||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
Line 4,710: | Line 4,710: | ||
(apply vc-append 2 (map draw-row (reverse rows))))) |
(apply vc-append 2 (map draw-row (reverse rows))))) |
||
(draw-automaton 104 '(0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0) 10)</ |
(draw-automaton 104 '(0 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0) 10)</syntaxhighlight> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 4,722: | Line 4,722: | ||
and it makes the implementation a lot easier. |
and it makes the implementation a lot easier. |
||
<lang |
<syntaxhighlight lang="raku" line>class Automaton { |
||
has $.rule; |
has $.rule; |
||
has @.cells; |
has @.cells; |
||
Line 4,757: | Line 4,757: | ||
$a = Automaton.new: :rule(90), :cells(flat @padding, 1, @padding); |
$a = Automaton.new: :rule(90), :cells(flat @padding, 1, @padding); |
||
say $a++ for ^20;</ |
say $a++ for ^20;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,795: | Line 4,795: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="rebol">Red [ |
||
Purpose: "One-dimensional cellular automata" |
Purpose: "One-dimensional cellular automata" |
||
Author: "Joe Smith" |
Author: "Joe Smith" |
||
Line 4,821: | Line 4,821: | ||
evo display [1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0] |
evo display [1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0] |
||
] |
] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 4,838: | Line 4,838: | ||
=={{header|Retro}}== |
=={{header|Retro}}== |
||
< |
<syntaxhighlight lang="retro"># 1D Cellular Automota |
||
Assume an array of cells with an initial distribution of live and |
Assume an array of cells with an initial distribution of live and |
||
Line 4,993: | Line 4,993: | ||
~~~ |
~~~ |
||
#10 generations |
#10 generations |
||
~~~</ |
~~~</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This REXX version will show (as a default) 40 generations, or less if the generations of cellular automata repeat. |
This REXX version will show (as a default) 40 generations, or less if the generations of cellular automata repeat. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program generates & displays N generations of one─dimensional cellular automata. */ |
||
parse arg $ gens . /*obtain optional arguments from the CL*/ |
parse arg $ gens . /*obtain optional arguments from the CL*/ |
||
if $=='' | $=="," then $=001110110101010 /*Not specified? Then use the default.*/ |
if $=='' | $=="," then $=001110110101010 /*Not specified? Then use the default.*/ |
||
Line 5,012: | Line 5,012: | ||
if $==@ then do; say right('repeats', 40); leave; end /*does it repeat? */ |
if $==@ then do; say right('repeats', 40); leave; end /*does it repeat? */ |
||
$=@ /*now use the next generation of cells.*/ |
$=@ /*now use the next generation of cells.*/ |
||
end /*#*/ /*stick a fork in it, we're all done. */</ |
end /*#*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
'''output''' when using the default input: |
'''output''' when using the default input: |
||
<pre> |
<pre> |
||
Line 5,026: | Line 5,026: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : One-dimensional cellular automata |
# Project : One-dimensional cellular automata |
||
Line 5,051: | Line 5,051: | ||
next |
next |
||
return binsum |
return binsum |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 5,067: | Line 5,067: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">def evolve(ary) |
||
([0]+ary+[0]).each_cons(3).map{|a,b,c| a+b+c == 2 ? 1 : 0} |
([0]+ary+[0]).each_cons(3).map{|a,b,c| a+b+c == 2 ? 1 : 0} |
||
end |
end |
||
Line 5,079: | Line 5,079: | ||
until ary == (new = evolve(ary)) |
until ary == (new = evolve(ary)) |
||
printit ary = new |
printit ary = new |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>.###.##.#.#.#.#..#.. |
<pre>.###.##.#.#.#.#..#.. |
||
Line 5,092: | Line 5,092: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn get_new_state(windowed: &[bool]) -> bool { |
||
match windowed { |
match windowed { |
||
[false, true, true] | [true, true, false] => true, |
[false, true, true] | [true, true, false] => true, |
||
Line 5,129: | Line 5,129: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{works with|Scala|2.8}} |
{{works with|Scala|2.8}} |
||
< |
<syntaxhighlight lang="scala">def cellularAutomata(s: String) = { |
||
def it = Iterator.iterate(s) ( generation => |
def it = Iterator.iterate(s) ( generation => |
||
("_%s_" format generation).iterator |
("_%s_" format generation).iterator |
||
Line 5,143: | Line 5,143: | ||
(it drop 1) zip it takeWhile Function.tupled(_ != _) map (_._2) foreach println |
(it drop 1) zip it takeWhile Function.tupled(_ != _) map (_._2) foreach println |
||
}</ |
}</syntaxhighlight> |
||
Sample: |
Sample: |
||
Line 5,161: | Line 5,161: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
{{Works with|Scheme|R<math>^5</math>RS}} |
{{Works with|Scheme|R<math>^5</math>RS}} |
||
< |
<syntaxhighlight lang="scheme">(define (next-generation left petri-dish right) |
||
(if (null? petri-dish) |
(if (null? petri-dish) |
||
(list) |
(list) |
||
Line 5,181: | Line 5,181: | ||
(- generations 1))))) |
(- generations 1))))) |
||
(display-evolution (list 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0) 10)</ |
(display-evolution (list 1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0) 10)</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>(1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0) |
<pre>(1 1 1 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0) |
||
Line 5,197: | Line 5,197: | ||
A graphical cellular automaton can be found [http://seed7.sourceforge.net/algorith/graphic.htm#cellauto here]. |
A graphical cellular automaton can be found [http://seed7.sourceforge.net/algorith/graphic.htm#cellauto here]. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const string: start is "_###_##_#_#_#_#__#__"; |
const string: start is "_###_##_#_#_#_#__#__"; |
||
Line 5,222: | Line 5,222: | ||
g0 := g1; |
g0 := g1; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
Output: |
Output: |
||
Line 5,263: | Line 5,263: | ||
=={{header|SequenceL}}== |
=={{header|SequenceL}}== |
||
< |
<syntaxhighlight lang="sequencel">import <Utilities/Conversion.sl>; |
||
main(args(2)) := |
main(args(2)) := |
||
Line 5,292: | Line 5,292: | ||
1 when (left + cells[i] + right) = 2 |
1 when (left + cells[i] + right) = 2 |
||
else |
else |
||
0;</ |
0;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,311: | Line 5,311: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">var seq = "_###_##_#_#_#_#__#__"; |
||
var x = ''; |
var x = ''; |
||
Line 5,320: | Line 5,320: | ||
seq.gsub!(/(?<=(.))(.)(?=(.))/, {|s1,s2,s3| s1 == s3 ? (s1 ? 1-s2 : 0) : s2}); |
seq.gsub!(/(?<=(.))(.)(?=(.))/, {|s1,s2,s3| s1 == s3 ? (s1 ? 1-s2 : 0) : s2}); |
||
(x != seq) && (x = seq) || break; |
(x != seq) && (x = seq) || break; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,336: | Line 5,336: | ||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">class Automaton(rule, cells) { |
||
method init { |
method init { |
||
Line 5,368: | Line 5,368: | ||
say "|#{auto}|"; |
say "|#{auto}|"; |
||
auto.next; |
auto.next; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,385: | Line 5,385: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc evolve {a} { |
||
set new [list] |
set new [list] |
||
for {set i 0} {$i < [llength $a]} {incr i} { |
for {set i 0} {$i < [llength $a]} {incr i} { |
||
Line 5,420: | Line 5,420: | ||
set array $new |
set array $new |
||
print $array |
print $array |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 5,429: | Line 5,429: | ||
it according to the rule. The cells are maintained as a list of booleans |
it according to the rule. The cells are maintained as a list of booleans |
||
(0 and &) but are converted to characters for presentation in the example code. |
(0 and &) but are converted to characters for presentation in the example code. |
||
< |
<syntaxhighlight lang="ursala">#import std |
||
#import nat |
#import nat |
||
Line 5,440: | Line 5,440: | ||
#show+ |
#show+ |
||
example = ~&?(`#!,`.!)** evolve10 <0,&,&,&,0,&,&,0,&,0,&,0,&,0,0,&,0,0></ |
example = ~&?(`#!,`.!)** evolve10 <0,&,&,&,0,&,&,0,&,0,&,0,&,0,0,&,0,0></syntaxhighlight> |
||
output: |
output: |
||
<pre> |
<pre> |
||
Line 5,458: | Line 5,458: | ||
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. |
||
< |
<syntaxhighlight lang="vedit">IT("Gen 0: ..###.##.#.#.#.#..#.....") // initial pattern |
||
#9 = Cur_Col |
#9 = Cur_Col |
||
Line 5,475: | Line 5,475: | ||
IT("Gen ") Num_Ins(#8, LEFT+NOCR) IT(": ") |
IT("Gen ") Num_Ins(#8, LEFT+NOCR) IT(": ") |
||
Reg_Ins(20) |
Reg_Ins(20) |
||
}</ |
}</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
< |
<syntaxhighlight lang="vedit">Gen 0: ..###.##.#.#.#.#..#..... |
||
Gen 1: ..#.#####.#.#.#......... |
Gen 1: ..#.#####.#.#.#......... |
||
Gen 2: ...##...##.#.#.......... |
Gen 2: ...##...##.#.#.......... |
||
Line 5,487: | Line 5,487: | ||
Gen 7: ...##.....#............. |
Gen 7: ...##.....#............. |
||
Gen 8: ...##................... |
Gen 8: ...##................... |
||
Gen 9: ...##...................</ |
Gen 9: ...##...................</syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Line 5,493: | Line 5,493: | ||
This implementation is run from the command line. The command is followed by a string of either 1's or #'s for an active cell, or 0's or _'s for an inactive one. |
This implementation is run from the command line. The command is followed by a string of either 1's or #'s for an active cell, or 0's or _'s for an inactive one. |
||
< |
<syntaxhighlight lang="visual basic .net">Imports System.Text |
||
Module CellularAutomata |
Module CellularAutomata |
||
Line 5,597: | Line 5,597: | ||
Return sw.ToString() |
Return sw.ToString() |
||
End Function |
End Function |
||
End Module</ |
End Module</syntaxhighlight> |
||
Output: |
Output: |
||
Line 5,614: | Line 5,614: | ||
=={{header|Wart}}== |
=={{header|Wart}}== |
||
===Simple=== |
===Simple=== |
||
< |
<syntaxhighlight lang="python">def (gens n l) |
||
prn l |
prn l |
||
repeat n |
repeat n |
||
Line 5,630: | Line 5,630: | ||
def (next a b c) # next state of b given neighbors a and c |
def (next a b c) # next state of b given neighbors a and c |
||
if (and a c) not.b |
if (and a c) not.b |
||
(or a c) b</ |
(or a c) b</syntaxhighlight> |
||
Output looks a little ugly: |
Output looks a little ugly: |
||
Line 5,647: | Line 5,647: | ||
Computing the next generation becomes much cleaner once you invest a few LoC in a new datatype. |
Computing the next generation becomes much cleaner once you invest a few LoC in a new datatype. |
||
< |
<syntaxhighlight lang="python">def (uca l) # new datatype: Uni-dimensional Cellular Automaton |
||
(tag uca (list l len.l)) |
(tag uca (list l len.l)) |
||
Line 5,678: | Line 5,678: | ||
def (next a b c) |
def (next a b c) |
||
if (and a c) not.b |
if (and a c) not.b |
||
(or a c) b</ |
(or a c) b</syntaxhighlight> |
||
Output is prettier now: |
Output is prettier now: |
||
Line 5,699: | Line 5,699: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="ecmascript">var trans = "___#_##_" |
||
var v = Fn.new { |cell, i| (cell[i] != "_") ? 1 : 0 } |
var v = Fn.new { |cell, i| (cell[i] != "_") ? 1 : 0 } |
||
Line 5,721: | Line 5,721: | ||
System.print(c[1..-1].join()) |
System.print(c[1..-1].join()) |
||
if (!evolve.call(c,b)) break |
if (!evolve.call(c,b)) break |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,737: | Line 5,737: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">code ChOut=8, CrLf=9; |
||
int Gen, Now, New, I; |
int Gen, Now, New, I; |
||
[Now:= $076A_A400; |
[Now:= $076A_A400; |
||
Line 5,748: | Line 5,748: | ||
Now:= New; |
Now:= New; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,766: | Line 5,766: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Locomotive_Basic}} |
{{trans|Locomotive_Basic}} |
||
< |
<syntaxhighlight lang="yabasic">10 n=10:READ w:DIM x(w+1),x2(w+1):FOR i=1 to w:READ x(i):NEXT |
||
20 FOR k=1 TO n |
20 FOR k=1 TO n |
||
30 FOR j=1 TO w |
30 FOR j=1 TO w |
||
Line 5,774: | Line 5,774: | ||
70 FOR j=1 TO w:x(j)=x2(j):NEXT |
70 FOR j=1 TO w:x(j)=x2(j):NEXT |
||
80 NEXT |
80 NEXT |
||
90 DATA 20,0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0</ |
90 DATA 20,0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0</syntaxhighlight> |
||
Other solution |
Other solution |
||
< |
<syntaxhighlight lang="yabasic">start$ = "0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,0,0,1,0,0" |
||
dim x$(1) |
dim x$(1) |
||
Line 5,791: | Line 5,791: | ||
next j |
next j |
||
print |
print |
||
next k</ |
next k</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Groovy}} |
{{trans|Groovy}} |
||
< |
<syntaxhighlight lang="zkl">fcn life1D(line){ |
||
right:=line[1,*] + False; // shift left, False fill |
right:=line[1,*] + False; // shift left, False fill |
||
left :=T(False).extend(line[0,-1]); // shift right |
left :=T(False).extend(line[0,-1]); // shift right |
||
left.zip(line,right).apply(fcn(hood){ hood.sum(0)==2 }); |
left.zip(line,right).apply(fcn(hood){ hood.sum(0)==2 }); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">chars:=T("_","#"); |
||
cells:="_###_##_#_#_#_#__#__".split("").apply('==("#")); //-->L(False,True,True,True,False...) |
cells:="_###_##_#_#_#_#__#__".split("").apply('==("#")); //-->L(False,True,True,True,False...) |
||
do(10){ cells.apply(chars.get).concat().println(); cells=life1D(cells); }</ |
do(10){ cells.apply(chars.get).concat().println(); cells=life1D(cells); }</syntaxhighlight> |
||
Or, using strings instead of lists: |
Or, using strings instead of lists: |
||
< |
<syntaxhighlight lang="zkl">fcn life1D(line){ |
||
right:=line[1,*] + "_"; // shift left, "_" fill |
right:=line[1,*] + "_"; // shift left, "_" fill |
||
left :="_" + line[0,-1]; // shift right |
left :="_" + line[0,-1]; // shift right |
||
Line 5,810: | Line 5,810: | ||
fcn(a,b,c){ (String(a,b,c) - "_") == "##" and "#" or "_" }, |
fcn(a,b,c){ (String(a,b,c) - "_") == "##" and "#" or "_" }, |
||
left,line,right).concat(); |
left,line,right).concat(); |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">cells:="_###_##_#_#_#_#__#__"; |
||
do(10){ cells.println(); cells=life1D(cells); }</ |
do(10){ cells.println(); cells=life1D(cells); }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |