Skip to content

Commit

Permalink
Example: add rule 110
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Mar 26, 2024
1 parent 41dd2e4 commit fb644a7
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions example/rule110.no
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::iter::MapAdapter

pub fn main() {
let n = 30
rule110(n)
}

fn rule110(n: Int) {
range(1, n).fold(|gen, _| {
let ng = nextGen(gen)
println(fmtGen(ng, n))
ng
}, [true])
unit
}

fn nextGen(prev: List<Bool>): List<Bool> {
range(-1, prev.iter().count())
.map(|i| {
let left = prev.at(i - 1).or(Some(false))!
let mid = prev.at(i).or(Some(false))!
let right = prev.at(i + 1).or(Some(false))!
return (
(left && mid && right) ||
(left && mid.not() && right.not()) ||
(left.not() && mid.not() && right.not())
).not()
})
.collect<List<Bool>>()
}

fn fmtGen(gen: List<Bool>, total: Int): String {
let pad = repeat(" ", total - gen.iter().count()).collect<String>()
let g = gen
.iter()
.map(|b| { if b { "x" } else { " " } })
.collect<String>()
pad.concat(g)
}

0 comments on commit fb644a7

Please sign in to comment.