Last active
May 20, 2023 22:09
-
-
Save peheje/8076f619f6fbc1202954bc70720ef986 to your computer and use it in GitHub Desktop.
Simple automata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://atlas.wolfram.com/01/01/ | |
let on = 1uy | |
let off = 0uy | |
let print xs = | |
for x in xs do if x = on then printf "X" else printf " " | |
printf "\n" | |
let rule0 p _ r = (p + r) % 2uy | |
let rule51 _ q _ = (on + q) % 2uy | |
let rule57 p q r = (on + q + r + p*r) % 2uy | |
let rule62 p q r = (p + q + r + p*r + q*r + p*r*q) % 2uy | |
let rule105 p q r = (on + p + q + r) % 2uy | |
let rule129 p q r = (on + p + q + p*q + r + p*r + q*r) % 2uy | |
let rule205 p _ r = (p + r - p*r) % 2uy | |
let rule225 p q r = (on + p + q + r + q * r) % 2uy | |
let advance src (dst: byte array) = | |
for i in 1..(Array.length src - 2) do | |
dst[i] <- rule0 src[i-1] src[i] src[i+1] | |
let width = 180 | |
let xs = Array.init width (fun i -> if i = width/2 then on else off) | |
let ys = Array.zeroCreate<byte> width | |
for i in 0..100 do | |
if i % 2 = 0 then | |
advance xs ys | |
print xs | |
else | |
advance ys xs | |
print ys |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
var on byte = 1 | |
var off byte = 0 | |
func print(xs []byte) { | |
for _, x := range xs { | |
if x == on { | |
fmt.Print("#") | |
} else { | |
fmt.Print(" ") | |
} | |
} | |
fmt.Println() | |
} | |
func advance(xs []byte) { | |
cp := make([]byte, len(xs)) | |
copy(cp, xs) | |
for i := 1; i < len(xs)-1; i++ { | |
xs[i] = (cp[i-1] + cp[i+1]) % 2 | |
} | |
} | |
func main() { | |
width := 100 | |
iterations := 1000 | |
xs := make([]byte, width) | |
for i := 0; i < width; i++ { | |
xs[i] = off | |
} | |
xs[width/2] = on | |
for i := 0; i < iterations; i++ { | |
print(xs) | |
advance(xs) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sequtils | |
import random | |
const | |
on = 1 | |
off = 0 | |
width = 100 | |
iterations = 100 | |
proc advance(xs: seq[int]): seq[int] = | |
result = xs | |
for i in 1..<xs.len-1: | |
result[i] = (xs[i-1] + xs[i+1]) mod 2 | |
proc print(xs: seq[int]) = | |
for x in xs: | |
if x == off: stdout.write(" ") | |
else: stdout.write("#") | |
echo "" | |
proc main() = | |
# Start off with one in the middle | |
var xs = newSeqWith(width, off) | |
xs[width div 2] = on | |
# Or start off random | |
#var xs = newSeqWith(width, rand(1)) | |
for i in 0..<iterations: | |
xs.print | |
xs = advance(xs) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment