
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1205 · main · 10 min 08 s
1Étoiles
0Forks
0Issues
| 1 | // Allocation: build and walk many binary trees (after the Computer |
| 2 | // Language Benchmarks Game's binary-trees). Each tree holds its round's |
| 3 | // number, so that no compiler builds it only once. |
| 4 | import gleam/int |
| 5 | import gleam/io |
| 6 | |
| 7 | type Tree { |
| 8 | Leaf(Int) |
| 9 | Node(Tree, Tree) |
| 10 | } |
| 11 | |
| 12 | fn make(d: Int, v: Int) -> Tree { |
| 13 | case d == 0 { |
| 14 | True -> Leaf(v) |
| 15 | False -> Node(make(d - 1, v), make(d - 1, v)) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | fn check(t: Tree) -> Int { |
| 20 | case t { |
| 21 | Leaf(v) -> case int.remainder(v, 2) { |
| 22 | Ok(r) -> r |
| 23 | _ -> 0 |
| 24 | } |
| 25 | Node(l, r) -> 1 + check(l) + check(r) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | fn pow2(k: Int) -> Int { |
| 30 | case k == 0 { |
| 31 | True -> 1 |
| 32 | False -> 2 * pow2(k - 1) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn repeat(i: Int, d: Int, acc: Int) -> Int { |
| 37 | case i == 0 { |
| 38 | True -> acc |
| 39 | False -> repeat(i - 1, d, acc + check(make(d, i))) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | fn depths(d: Int, max: Int, acc: Int) -> Int { |
| 44 | case d > max { |
| 45 | True -> acc |
| 46 | False -> depths(d + 2, max, acc + repeat(pow2(max - d + 4), d, 0)) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub fn main() { |
| 51 | io.println(int.to_string(depths(4, 16, 0))) |
| 52 | } |