// Allocation: build and walk many binary trees (after the Computer // Language Benchmarks Game's binary-trees). Each tree holds its round's // number, so that no compiler builds it only once. import gleam/int import gleam/io type Tree { Leaf(Int) Node(Tree, Tree) } fn make(d: Int, v: Int) -> Tree { case d == 0 { True -> Leaf(v) False -> Node(make(d - 1, v), make(d - 1, v)) } } fn check(t: Tree) -> Int { case t { Leaf(v) -> case int.remainder(v, 2) { Ok(r) -> r _ -> 0 } Node(l, r) -> 1 + check(l) + check(r) } } fn pow2(k: Int) -> Int { case k == 0 { True -> 1 False -> 2 * pow2(k - 1) } } fn repeat(i: Int, d: Int, acc: Int) -> Int { case i == 0 { True -> acc False -> repeat(i - 1, d, acc + check(make(d, i))) } } fn depths(d: Int, max: Int, acc: Int) -> Int { case d > max { True -> acc False -> depths(d + 2, max, acc + repeat(pow2(max - d + 4), d, 0)) } } pub fn main() { io.println(int.to_string(depths(4, 16, 0))) }