louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 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 | package main |
| 5 | |
| 6 | import "fmt" |
| 7 | |
| 8 | type tree struct { |
| 9 | leaf bool |
| 10 | value int64 |
| 11 | left *tree |
| 12 | right *tree |
| 13 | } |
| 14 | |
| 15 | func makeTree(d, v int64) *tree { |
| 16 | if d == 0 { |
| 17 | return &tree{leaf: true, value: v} |
| 18 | } |
| 19 | return &tree{left: makeTree(d-1, v), right: makeTree(d-1, v)} |
| 20 | } |
| 21 | |
| 22 | func check(t *tree) int64 { |
| 23 | if t.leaf { |
| 24 | return t.value % 2 |
| 25 | } |
| 26 | return 1 + check(t.left) + check(t.right) |
| 27 | } |
| 28 | |
| 29 | func pow2(k int64) int64 { |
| 30 | if k == 0 { |
| 31 | return 1 |
| 32 | } |
| 33 | return 2 * pow2(k-1) |
| 34 | } |
| 35 | |
| 36 | func repeat(i, d, acc int64) int64 { |
| 37 | if i == 0 { |
| 38 | return acc |
| 39 | } |
| 40 | return repeat(i-1, d, acc+check(makeTree(d, i))) |
| 41 | } |
| 42 | |
| 43 | func depths(d, max, acc int64) int64 { |
| 44 | if d > max { |
| 45 | return acc |
| 46 | } |
| 47 | return depths(d+2, max, acc+repeat(pow2(max-d+4), d, 0)) |
| 48 | } |
| 49 | |
| 50 | func main() { |
| 51 | fmt.Println(depths(4, 16, 0)) |
| 52 | } |