Elementary cellular automaton: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 21: | Line 21: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">V SIZE = 32 |
||
V LINES = SIZE I/ 2 |
V LINES = SIZE I/ 2 |
||
V RULE = 90 |
V RULE = 90 |
||
Line 48: | Line 48: | ||
L 1..LINES |
L 1..LINES |
||
show(state) |
show(state) |
||
evolve(&state)</ |
evolve(&state)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 72: | Line 72: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">DEFINE ROW_LEN="320" |
||
DEFINE MAX_COL="319" |
DEFINE MAX_COL="319" |
||
DEFINE MAX_ROW="191" |
DEFINE MAX_ROW="191" |
||
Line 158: | Line 158: | ||
DO UNTIL CH#$FF OD |
DO UNTIL CH#$FF OD |
||
CH=$FF |
CH=$FF |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Elementary_cellular_automaton.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Elementary_cellular_automaton.png Screenshot from Atari 8-bit computer] |
||
Line 164: | Line 164: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|Ada 2012}} |
{{works with|Ada 2012}} |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Elementary_Cellular_Automaton is |
procedure Elementary_Cellular_Automaton is |
||
Line 230: | Line 230: | ||
Generations => 25); |
Generations => 25); |
||
end Elementary_Cellular_Automaton; |
end Elementary_Cellular_Automaton; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre>Rule 90 : |
<pre>Rule 90 : |
||
Line 297: | Line 297: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN # elementary cellular automaton # |
||
COMMENT returns the next state from state using rule; s must be at least 2 characters long and consist of # and - only COMMENT |
COMMENT returns the next state from state using rule; s must be at least 2 characters long and consist of # and - only COMMENT |
||
PROC next state = ( STRING state, INT rule )STRING: |
PROC next state = ( STRING state, INT rule )STRING: |
||
Line 339: | Line 339: | ||
test( "---------#---------", 60 ); |
test( "---------#---------", 60 ); |
||
test( "---------#---------", 90 ) |
test( "---------#---------", 90 ) |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 382: | Line 382: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{works with|AutoHotkey 1.1}} |
{{works with|AutoHotkey 1.1}} |
||
< |
<syntaxhighlight lang="autohotkey">state := StrSplit("0000000001000000000") |
||
rule := 90 |
rule := 90 |
||
output := "Rule: " rule |
output := "Rule: " rule |
||
Line 428: | Line 428: | ||
result .= val = 1 ? "#" : "." |
result .= val = 1 ? "#" : "." |
||
return result |
return result |
||
}</ |
}</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre>Rule: 90 |
<pre>Rule: 90 |
||
Line 444: | Line 444: | ||
=={{header|C}}== |
=={{header|C}}== |
||
64 cells, edges are cyclic. |
64 cells, edges are cyclic. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <limits.h> |
#include <limits.h> |
||
Line 474: | Line 474: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 491: | Line 491: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
using System.Collections; |
using System.Collections; |
||
Line 584: | Line 584: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 644: | Line 644: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <bitset> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 674: | Line 674: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> # | |
<pre> # | |
||
Line 688: | Line 688: | ||
=={{header|Ceylon}}== |
=={{header|Ceylon}}== |
||
< |
<syntaxhighlight lang="ceylon">class Rule(number) satisfies Correspondence<Boolean[3], Boolean> { |
||
shared Byte number; |
shared Byte number; |
||
Line 773: | Line 773: | ||
automaton.evolve(); |
automaton.evolve(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Rule #90 |
<pre>Rule #90 |
||
Line 791: | Line 791: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun automaton (init rule &optional (stop 10)) |
||
(labels ((next-gen (cells) |
(labels ((next-gen (cells) |
||
(mapcar #'new-cell |
(mapcar #'new-cell |
||
Line 811: | Line 811: | ||
do (pretty-print cells)))) |
do (pretty-print cells)))) |
||
(automaton '(0 0 0 0 0 0 1 0 0 0 0 0 0) 90)</ |
(automaton '(0 0 0 0 0 0 1 0 0 0 0 0 0) 90)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 827: | Line 827: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.range, std.algorithm, std.typecons; |
||
enum mod = (in int n, in int m) pure nothrow @safe @nogc => ((n % m) + m) % m; |
enum mod = (in int n, in int m) pure nothrow @safe @nogc => ((n % m) + m) % m; |
||
Line 862: | Line 862: | ||
eca.popFront; |
eca.popFront; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Rules: [90, 30, 122] |
<pre>Rules: [90, 30, 122] |
||
Line 919: | Line 919: | ||
Pictures of the (nice) generated colored bit-maps : The Escher like [http://www.echolalie.org/echolisp/images/automaton-1.png (task 90 5)] and the fractal like [http://www.echolalie.org/echolisp/images/automaton-2.png (task 22 1)] |
Pictures of the (nice) generated colored bit-maps : The Escher like [http://www.echolalie.org/echolisp/images/automaton-1.png (task 90 5)] and the fractal like [http://www.echolalie.org/echolisp/images/automaton-2.png (task 22 1)] |
||
< |
<syntaxhighlight lang="scheme"> |
||
(lib 'types) ;; int32 vectors |
(lib 'types) ;; int32 vectors |
||
(lib 'plot) |
(lib 'plot) |
||
Line 979: | Line 979: | ||
→ #( #( 0 0 0) #( 0 -5052980 0) #( 0 -5052980 -5052980)) |
→ #( #( 0 0 0) #( 0 -5052980 0) #( 0 -5052980 -5052980)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{works with|Elixir|1.3}} |
{{works with|Elixir|1.3}} |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir">defmodule Elementary_cellular_automaton do |
||
def run(start_str, rule, times) do |
def run(start_str, rule, times) do |
||
IO.puts "rule : #{rule}" |
IO.puts "rule : #{rule}" |
||
Line 1,010: | Line 1,010: | ||
pad = String.duplicate("0", 14) |
pad = String.duplicate("0", 14) |
||
str = pad <> "1" <> pad |
str = pad <> "1" <> pad |
||
Elementary_cellular_automaton.run(str, 18, 25)</ |
Elementary_cellular_automaton.run(str, 18, 25)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,044: | Line 1,044: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
===The Function=== |
===The Function=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Elementary Cellular Automaton . Nigel Galloway: July 31st., 2019 |
// Elementary Cellular Automaton . Nigel Galloway: July 31st., 2019 |
||
let eca N= |
let eca N= |
||
let N=Array.init 8 (fun n->(N>>>n)%2) |
let N=Array.init 8 (fun n->(N>>>n)%2) |
||
Seq.unfold(fun G->Some(G,[|yield Array.last G; yield! G; yield Array.head G|]|>Array.windowed 3|>Array.map(fun n->N.[n.[2]+2*n.[1]+4*n.[0]]))) |
Seq.unfold(fun G->Some(G,[|yield Array.last G; yield! G; yield Array.head G|]|>Array.windowed 3|>Array.map(fun n->N.[n.[2]+2*n.[1]+4*n.[0]]))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
===The Task=== |
===The Task=== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
eca 90 [|0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0|] |> Seq.take 80 |> Seq.iter(fun n->Array.iter(fun n->printf "%s" (if n=0 then " " else "@"))n; printfn "") |
eca 90 [|0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0|] |> Seq.take 80 |> Seq.iter(fun n->Array.iter(fun n->printf "%s" (if n=0 then " " else "@"))n; printfn "") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,137: | Line 1,137: | ||
@ @ |
@ @ |
||
</pre> |
</pre> |
||
< |
<syntaxhighlight lang="fsharp"> |
||
eca 110 [|0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1|] |> Seq.take 80 |> Seq.iter(fun n->Array.iter(fun n->printf "%s" (if n=0 then " " else "@"))n; printfn "") |
eca 110 [|0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;1|] |> Seq.take 80 |> Seq.iter(fun n->Array.iter(fun n->printf "%s" (if n=0 then " " else "@"))n; printfn "") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,225: | Line 1,225: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: assocs formatting grouping io kernel math math.bits |
||
math.combinatorics sequences sequences.extras ; |
math.combinatorics sequences sequences.extras ; |
||
Line 1,242: | Line 1,242: | ||
[ dup show-state next-state ] times 2drop ; |
[ dup show-state next-state ] times 2drop ; |
||
90 show-automaton</ |
90 show-automaton</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,266: | Line 1,266: | ||
{{works with|gforth|0.7.3}} |
{{works with|gforth|0.7.3}} |
||
<br> |
<br> |
||
< |
<syntaxhighlight lang="forth">#! /usr/bin/gforth |
||
\ Elementary cellular automaton |
\ Elementary cellular automaton |
||
Line 1,338: | Line 1,338: | ||
bye |
bye |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,348: | Line 1,348: | ||
* rule 110 [https://commons.wikimedia.org/wiki/File:Rule-110.png] |
* rule 110 [https://commons.wikimedia.org/wiki/File:Rule-110.png] |
||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic"> |
||
#define NCELLS 400 |
#define NCELLS 400 |
||
#define border 16 |
#define border 16 |
||
Line 1,416: | Line 1,416: | ||
end if |
end if |
||
key = ucase(inkey) |
key = ucase(inkey) |
||
loop until ucase(key) = "Q"</ |
loop until ucase(key) = "Q"</syntaxhighlight> |
||
=={{header|Fōrmulæ}}== |
=={{header|Fōrmulæ}}== |
||
Line 1,427: | Line 1,427: | ||
=={{header|Furor}}== |
=={{header|Furor}}== |
||
<syntaxhighlight lang="furor"> |
|||
<lang Furor> |
|||
argc 4 < { ."Usage: " 0 argv sprint SPACE 1 argv sprint SPACE ."rule size\n" |
argc 4 < { ."Usage: " 0 argv sprint SPACE 1 argv sprint SPACE ."rule size\n" |
||
."The \"rule\" and \"size\" are numbers.\n" |
."The \"rule\" and \"size\" are numbers.\n" |
||
Line 1,471: | Line 1,471: | ||
// ================================================= |
// ================================================= |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,515: | Line 1,515: | ||
=={{header|GFA Basic}}== |
=={{header|GFA Basic}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
' |
' |
||
' Elementary One-Dimensional Cellular Automaton |
' Elementary One-Dimensional Cellular Automaton |
||
Line 1,641: | Line 1,641: | ||
RETURN result% |
RETURN result% |
||
ENDFUNC |
ENDFUNC |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,688: | Line 1,688: | ||
output() |
output() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,720: | Line 1,720: | ||
Straight-forward implementation of CA on a cyclic domain, using immutable arrays: |
Straight-forward implementation of CA on a cyclic domain, using immutable arrays: |
||
< |
<syntaxhighlight lang="haskell">import Data.Array (listArray, (!), bounds, elems) |
||
step rule a = listArray (l,r) res |
step rule a = listArray (l,r) res |
||
Line 1,728: | Line 1,728: | ||
[rule (a!(r-1)) (a!r) (a!l) ] |
[rule (a!(r-1)) (a!r) (a!l) ] |
||
runCA rule = iterate (step rule)</ |
runCA rule = iterate (step rule)</syntaxhighlight> |
||
The following gives decoding of the CA rule and prepares the initial CA state: |
The following gives decoding of the CA rule and prepares the initial CA state: |
||
< |
<syntaxhighlight lang="haskell">rule n l x r = n `div` (2^(4*l + 2*x + r)) `mod` 2 |
||
initial n = listArray (0,n-1) . center . padRight n |
initial n = listArray (0,n-1) . center . padRight n |
||
where |
where |
||
padRight n lst = take n $ lst ++ repeat 0 |
padRight n lst = take n $ lst ++ repeat 0 |
||
center = take n . drop (n `div` 2+1) . cycle</ |
center = take n . drop (n `div` 2+1) . cycle</syntaxhighlight> |
||
Finally the IO stuff: |
Finally the IO stuff: |
||
< |
<syntaxhighlight lang="haskell">displayCA n rule init = mapM_ putStrLn $ take n result |
||
where result = fmap display . elems <$> runCA rule init |
where result = fmap display . elems <$> runCA rule init |
||
display 0 = ' ' |
display 0 = ' ' |
||
display 1 = '*'</ |
display 1 = '*'</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 1,792: | Line 1,792: | ||
The cyclic CA domain is represented by an infinite ''zipper list''. First we provide the datatype, the viewer and constructor: |
The cyclic CA domain is represented by an infinite ''zipper list''. First we provide the datatype, the viewer and constructor: |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE DeriveFunctor #-} |
||
import Control.Comonad |
import Control.Comonad |
||
Line 1,805: | Line 1,805: | ||
-- zero cycle length ensures that elements of the empty cycle will never be accessed |
-- zero cycle length ensures that elements of the empty cycle will never be accessed |
||
fromList lst = let x:::r = Inf.cycle lst |
fromList lst = let x:::r = Inf.cycle lst |
||
in Cycle (length lst) (last lst) x r</ |
in Cycle (length lst) (last lst) x r</syntaxhighlight> |
||
In order to run the CA on the domain we make it an instance of <code>Comonad</code> class. Running the CA turns to be just an iterative comonadic ''extension'' of the rule: |
In order to run the CA on the domain we make it an instance of <code>Comonad</code> class. Running the CA turns to be just an iterative comonadic ''extension'' of the rule: |
||
< |
<syntaxhighlight lang="haskell">instance Comonad Cycle where |
||
extract (Cycle _ _ x _) = x |
extract (Cycle _ _ x _) = x |
||
duplicate x@(Cycle n _ _ _) = fromList $ take n $ iterate shift x |
duplicate x@(Cycle n _ _ _) = fromList $ take n $ iterate shift x |
||
Line 1,816: | Line 1,816: | ||
step rule (Cycle _ l x (r:::_)) = rule l x r |
step rule (Cycle _ l x (r:::_)) = rule l x r |
||
runCA rule = iterate (=>> step rule)</ |
runCA rule = iterate (=>> step rule)</syntaxhighlight> |
||
Rule definition and I/O routine is the same as in Array-based solution: |
Rule definition and I/O routine is the same as in Array-based solution: |
||
< |
<syntaxhighlight lang="haskell">rule n l x r = n `div` (2^(4*l + 2*x + r)) `mod` 2 |
||
initial n lst = fromList $ center $ padRight n lst |
initial n lst = fromList $ center $ padRight n lst |
||
Line 1,831: | Line 1,831: | ||
where result = fmap display . view <$> runCA rule init |
where result = fmap display . view <$> runCA rule init |
||
display 0 = ' ' |
display 0 = ' ' |
||
display 1 = '*'</ |
display 1 = '*'</syntaxhighlight> |
||
See also [[Elementary cellular automaton/Infinite length#Haskell]] |
See also [[Elementary cellular automaton/Infinite length#Haskell]] |
||
Line 1,839: | Line 1,839: | ||
We'll define a state transition mechanism, and then rely on the language for iteration and display: |
We'll define a state transition mechanism, and then rely on the language for iteration and display: |
||
< |
<syntaxhighlight lang="j"> next=: ((8$2) #: [) {~ 2 #. 1 - [: |: |.~"1 0&_1 0 1@] |
||
' *'{~90 next^:(i.9) 0 0 0 0 0 0 1 0 0 0 0 0 |
' *'{~90 next^:(i.9) 0 0 0 0 0 0 1 0 0 0 0 0 |
||
* |
* |
||
Line 1,849: | Line 1,849: | ||
* * |
* * |
||
* * * * |
* * * * |
||
* * </ |
* * </syntaxhighlight> |
||
Or, we can view this on a larger scale, graphically: |
Or, we can view this on a larger scale, graphically: |
||
< |
<syntaxhighlight lang="j"> require'viewmat' |
||
viewmat 90 next^:(i.200) 0=i:200</ |
viewmat 90 next^:(i.200) 0=i:200</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|8}} |
{{works with|Java|8}} |
||
< |
<syntaxhighlight lang="java">import java.awt.*; |
||
import java.awt.event.ActionEvent; |
import java.awt.event.ActionEvent; |
||
import javax.swing.*; |
import javax.swing.*; |
||
Line 1,940: | Line 1,940: | ||
}); |
}); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
[[File:ca java.png|900px]] |
[[File:ca java.png|900px]] |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">const alive = '#'; |
||
const dead = '.'; |
const dead = '.'; |
||
Line 1,989: | Line 1,989: | ||
} |
} |
||
displayCA(90, 57, 31, 28);</ |
displayCA(90, 57, 31, 28);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,036: | Line 2,036: | ||
'''Helper functions''' |
'''Helper functions''' |
||
< |
<syntaxhighlight lang="jq"># The ordinal value of the relevant states: |
||
def states: |
def states: |
||
{"111": 1, "110": 2, "101": 3, "100": 4, "011": 5, "010": 6, "001": 7, "000": 8}; |
{"111": 1, "110": 2, "101": 3, "100": 4, "011": 5, "010": 6, "001": 7, "000": 8}; |
||
Line 2,062: | Line 2,062: | ||
| .[1:] # remove the leading 0 |
| .[1:] # remove the leading 0 |
||
| join("") |
| join("") |
||
end ;</ |
end ;</syntaxhighlight> |
||
'''Main function''' |
'''Main function''' |
||
< |
<syntaxhighlight lang="jq"># "rule" can be given as a decimal or string of 0s and 1s: |
||
def automaton(rule; steps): |
def automaton(rule; steps): |
||
Line 2,089: | Line 2,089: | ||
| gsub("0"; ".") # pretty print |
| gsub("0"; ".") # pretty print |
||
| gsub("1"; "#") |
| gsub("1"; "#") |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 2,108: | Line 2,108: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"> |
||
const lines = 10 |
const lines = 10 |
||
const start = ".........#........." |
const start = ".........#........." |
||
Line 2,137: | Line 2,137: | ||
end |
end |
||
end |
end |
||
</ |
</syntaxhighlight> {{output}} <pre> |
||
Using Rule 90: |
Using Rule 90: |
||
.........#......... |
.........#......... |
||
Line 2,177: | Line 2,177: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang="scala">// version 1.1.51 |
||
import java.util.BitSet |
import java.util.BitSet |
||
Line 2,214: | Line 2,214: | ||
evolve(state) |
evolve(state) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,238: | Line 2,238: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">local CA = { |
||
state = "..............................#..............................", |
state = "..............................#..............................", |
||
bstr = { [0]="...", "..#", ".#.", ".##", "#..", "#.#", "##.", "###" }, |
bstr = { [0]="...", "..#", ".#.", ".##", "#..", "#.#", "##.", "###" }, |
||
Line 2,263: | Line 2,263: | ||
print(string.format("%-66s%-66s%-66s%-61s", ca[1].state, ca[2].state, ca[3].state, ca[4].state)) |
print(string.format("%-66s%-66s%-66s%-61s", ca[1].state, ca[2].state, ca[3].state, ca[4].state)) |
||
for j = 1, 4 do ca[j]:evolve() end |
for j = 1, 4 do ca[j]:evolve() end |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style="font-size:50%">..............................#.............................. ..............................#.............................. ..............................#.............................. ..............................#.............................. |
<pre style="font-size:50%">..............................#.............................. ..............................#.............................. ..............................#.............................. ..............................#.............................. |
||
Line 2,333: | Line 2,333: | ||
Mathematica provides built-in functions for cellular automata. For example visualizing the first 100 rows of rule 30 on an 8-bit grid with a single initial cell: |
Mathematica provides built-in functions for cellular automata. For example visualizing the first 100 rows of rule 30 on an 8-bit grid with a single initial cell: |
||
< |
<syntaxhighlight lang="mathematica">ArrayPlot[CellularAutomaton[30, {0, 0, 0, 0, 1, 0, 0, 0}, 100]]</syntaxhighlight> |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">function init = cellularAutomaton(rule, init, n) |
||
init(n + 1, :) = 0; |
init(n + 1, :) = 0; |
||
for k = 1 : n |
for k = 1 : n |
||
init(k + 1, :) = bitget(rule, 1 + filter2([4 2 1], init(k, :))); |
init(k + 1, :) = bitget(rule, 1 + filter2([4 2 1], init(k, :))); |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="matlab">>> char(cellularAutomaton(90, ~(-15:15), 15) * 10 + 32) |
||
ans = |
ans = |
||
* |
* |
||
Line 2,359: | Line 2,359: | ||
* * * * * * * * |
* * * * * * * * |
||
* * * * * * * * |
* * * * * * * * |
||
* * * * * * * * * * * * * * * *</ |
* * * * * * * * * * * * * * * *</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="nim">import bitops |
||
const |
const |
||
Line 2,416: | Line 2,416: | ||
for _ in 1..Lines: |
for _ in 1..Lines: |
||
show(state) |
show(state) |
||
evolve(state)</ |
evolve(state)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,438: | Line 2,438: | ||
=={{header|Octave}}== |
=={{header|Octave}}== |
||
< |
<syntaxhighlight lang="octave">clear all |
||
E=200; |
E=200; |
||
idx=round(E/2); |
idx=round(E/2); |
||
Line 2,453: | Line 2,453: | ||
endfor |
endfor |
||
imagesc(reshape(z,E,E)'); % Medland map |
imagesc(reshape(z,E,E)'); % Medland map |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
Line 2,492: | Line 2,492: | ||
for (1..40) { |
for (1..40) { |
||
print "|$a|\n"; $a->next; |
print "|$a|\n"; $a->next; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>| # | |
<pre>| # | |
||
Line 2,537: | Line 2,537: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
String-based solution |
String-based solution |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</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: #0000FF;">,</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: #0000FF;">,</span> |
||
Line 2,556: | Line 2,556: | ||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span> |
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</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>--> |
||
Output matches that of D and Python:wrap for rule = 90, 30, 122 (if you edit/run 3 times) |
Output matches that of D and Python:wrap for rule = 90, 30, 122 (if you edit/run 3 times) |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de dictionary (N) |
||
(extract |
(extract |
||
'((A B) |
'((A B) |
||
Line 2,587: | Line 2,587: | ||
(cellular |
(cellular |
||
".........#........." |
".........#........." |
||
90 )</ |
90 )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,603: | Line 2,603: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
< |
<syntaxhighlight lang="prolog">play :- initial(I), do_auto(50, I). |
||
initial([0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]). |
initial([0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0]). |
||
Line 2,633: | Line 2,633: | ||
writ(0) :- write('.'). |
writ(0) :- write('.'). |
||
writ(1) :- write(1).</ |
writ(1) :- write(1).</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 2,640: | Line 2,640: | ||
:''You can deal with the limit conditions (what happens on the borders of the space) in any way you please.'' |
:''You can deal with the limit conditions (what happens on the borders of the space) in any way you please.'' |
||
< |
<syntaxhighlight lang="python">def eca(cells, rule): |
||
lencells = len(cells) |
lencells = len(cells) |
||
c = "0" + cells + "0" # Zero pad the ends |
c = "0" + cells + "0" # Zero pad the ends |
||
Line 2,660: | Line 2,660: | ||
i = data[0] |
i = data[0] |
||
cells = data[1:] |
cells = data[1:] |
||
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#')))</ |
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#')))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,718: | Line 2,718: | ||
===Python: wrap=== |
===Python: wrap=== |
||
The ends of the cells wrap-around. |
The ends of the cells wrap-around. |
||
< |
<syntaxhighlight lang="python">def eca_wrap(cells, rule): |
||
lencells = len(cells) |
lencells = len(cells) |
||
rulebits = '{0:08b}'.format(rule) |
rulebits = '{0:08b}'.format(rule) |
||
Line 2,735: | Line 2,735: | ||
cells = data[1:] |
cells = data[1:] |
||
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#'))) |
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#'))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,795: | Line 2,795: | ||
Pad and extend with inverse of end cells on each iteration. |
Pad and extend with inverse of end cells on each iteration. |
||
< |
<syntaxhighlight lang="python">def _notcell(c): |
||
return '0' if c == '1' else '1' |
return '0' if c == '1' else '1' |
||
Line 2,817: | Line 2,817: | ||
i = data[0] |
i = data[0] |
||
cells = ['%s%s%s' % (' '*(lines - i), c, ' '*(lines - i)) for c in data[1:]] |
cells = ['%s%s%s' % (' '*(lines - i), c, ' '*(lines - i)) for c in data[1:]] |
||
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#')))</ |
print('%2i: %s' % (i, ' '.join(cells).replace('0', '.').replace('1', '#')))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,849: | Line 2,849: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> ( the Cellular Automaton is on the stack as 3 items, the ) |
||
( Rule (R), the Size of the space (S) and the Current ) |
( Rule (R), the Size of the space (S) and the Current ) |
||
( state (C). make-ca sets this up from a string indicating ) |
( state (C). make-ca sets this up from a string indicating ) |
||
Line 2,902: | Line 2,902: | ||
say "Rule 30, 50 generations:" cr cr |
say "Rule 30, 50 generations:" cr cr |
||
$ ".........#........." 30 50 generations</ |
$ ".........#........." 30 50 generations</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,967: | Line 2,967: | ||
unmodified for [[Elementary cellular automaton/Infinite length]]. |
unmodified for [[Elementary cellular automaton/Infinite length]]. |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require racket/fixnum) |
(require racket/fixnum) |
||
(provide usable-bits/fixnum usable-bits/fixnum-1 CA-next-generation |
(provide usable-bits/fixnum usable-bits/fixnum-1 CA-next-generation |
||
Line 3,052: | Line 3,052: | ||
(show-automaton v #:step step #:sig-bits 19) |
(show-automaton v #:step step #:sig-bits 19) |
||
(newline) |
(newline) |
||
(ng/122/19-bits v o)))</ |
(ng/122/19-bits v o)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,104: | Line 3,104: | ||
Using the <tt>Automaton</tt> class defined at [[One-dimensional_cellular_automata#Raku]]: |
Using the <tt>Automaton</tt> class defined at [[One-dimensional_cellular_automata#Raku]]: |
||
<lang |
<syntaxhighlight lang="raku" line>class Automaton { |
||
has $.rule; |
has $.rule; |
||
has @.cells; |
has @.cells; |
||
Line 3,128: | Line 3,128: | ||
:cells(flat @padding, 1, @padding); |
:cells(flat @padding, 1, @padding); |
||
say $a++ for ^10;</ |
say $a++ for ^10;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,145: | Line 3,145: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class ElemCellAutomat |
||
include Enumerable |
include Enumerable |
||
Line 3,166: | Line 3,166: | ||
eca = ElemCellAutomat.new('1'.center(39, "0"), 18, true) |
eca = ElemCellAutomat.new('1'.center(39, "0"), 18, true) |
||
eca.take(30).each{|line| puts line.tr("01", ".#")}</ |
eca.take(30).each{|line| puts line.tr("01", ".#")}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,203: | Line 3,203: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
<syntaxhighlight lang="rust"> |
|||
<lang Rust> |
|||
fn main() { |
fn main() { |
||
struct ElementaryCA { |
struct ElementaryCA { |
||
Line 3,246: | Line 3,246: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,286: | Line 3,286: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===Java Swing Interoperability=== |
===Java Swing Interoperability=== |
||
< |
<syntaxhighlight lang="scala">import java.awt._ |
||
import java.awt.event.ActionEvent |
import java.awt.event.ActionEvent |
||
Line 3,358: | Line 3,358: | ||
}) |
}) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
< |
<syntaxhighlight lang="scheme">; uses SRFI-1 library http://srfi.schemers.org/srfi-1/srfi-1.html |
||
(define (evolve ls r) |
(define (evolve ls r) |
||
Line 3,389: | Line 3,389: | ||
n)) |
n)) |
||
(automaton '(0 1 0 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 1) 30 20)</ |
(automaton '(0 1 0 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 1) 30 20)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,417: | Line 3,417: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
< |
<syntaxhighlight lang="ruby">class Automaton(rule, cells) { |
||
method init { |
method init { |
||
Line 3,449: | Line 3,449: | ||
print "|#{auto}|\n" |
print "|#{auto}|\n" |
||
auto.next |
auto.next |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,466: | Line 3,466: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{works with|Tcl|8.6}} |
{{works with|Tcl|8.6}} |
||
< |
<syntaxhighlight lang="tcl">package require Tcl 8.6 |
||
oo::class create ElementaryAutomaton { |
oo::class create ElementaryAutomaton { |
||
Line 3,505: | Line 3,505: | ||
puts [string map "0 . 1 #" [join $s ""]] |
puts [string map "0 . 1 #" [join $s ""]] |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating: |
Demonstrating: |
||
< |
<syntaxhighlight lang="tcl">puts "Rule 90 (with default state):" |
||
ElementaryAutomaton create rule90 90 |
ElementaryAutomaton create rule90 90 |
||
rule90 run 20 |
rule90 run 20 |
||
puts "\nRule 122:" |
puts "\nRule 122:" |
||
[ElementaryAutomaton new 122] run 25 "..........#......…."</ |
[ElementaryAutomaton new 122] run 25 "..........#......…."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,570: | Line 3,570: | ||
{{works with|Bourne Again SHell}} |
{{works with|Bourne Again SHell}} |
||
{{works with|Korn Shell}} |
{{works with|Korn Shell}} |
||
< |
<syntaxhighlight lang="bash">function print_cells { |
||
local chars=(. '#') cell |
local chars=(. '#') cell |
||
for cell; do |
for cell; do |
||
Line 3,606: | Line 3,606: | ||
automaton "$@" |
automaton "$@" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> 0: ................#............... |
<pre> 0: ................#............... |
||
Line 3,629: | Line 3,629: | ||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/fmt" for Conv |
||
var SIZE = 32 |
var SIZE = 32 |
||
Line 3,660: | Line 3,660: | ||
show.call(state) |
show.call(state) |
||
evolve.call(state) |
evolve.call(state) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,684: | Line 3,684: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn rule(n){ n=n.toString(2); "00000000"[n.len() - 8,*] + n } |
||
fcn applyRule(rule,cells){ |
fcn applyRule(rule,cells){ |
||
cells=String(cells[-1],cells,cells[0]); // wrap cell ends |
cells=String(cells[-1],cells,cells[0]); // wrap cell ends |
||
(cells.len() - 2).pump(String,'wrap(n){ rule[7 - cells[n,3].toInt(2)] }) |
(cells.len() - 2).pump(String,'wrap(n){ rule[7 - cells[n,3].toInt(2)] }) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">cells:="0000000000000001000000000000000"; r90:=rule(90); map:=" *"; |
||
r90.println(" rule 90"); |
r90.println(" rule 90"); |
||
do(20){ cells.apply(map.get).println(); cells=applyRule(r90,cells); }</ |
do(20){ cells.apply(map.get).println(); cells=applyRule(r90,cells); }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |