-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy path1.hack
63 lines (55 loc) · 1.28 KB
/
1.hack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<<__EntryPoint>>
function main(): void {
$n = (int)(vec(\HH\global_get('argv') as Container<_>)[1] ?? 6);
$minDepth = 4;
$maxDepth = max($minDepth + 2, $n);
$stretchDepth = $maxDepth + 1;
$strechedTree = make($stretchDepth);
echo
"stretch tree of depth $stretchDepth\t check: ",
$strechedTree->check(),
PHP_EOL;
$longLivedTree = make($maxDepth);
$iterations = 1 << ($maxDepth);
do {
$check = 0;
for ($i = 1; $i <= $iterations; ++$i) {
$t = make($minDepth);
$check += $t->check();
}
printf(
"%d\t trees of depth %d\t check: %d\n",
$iterations,
$minDepth,
$check,
);
$minDepth += 2;
$iterations >>= 2;
} while ($minDepth <= $maxDepth);
echo
"long lived tree of depth $maxDepth\t check: ",
$longLivedTree->check(),
PHP_EOL;
}
class Node {
public ?Node $left = null;
public ?Node $right = null;
public function check(): int {
$v = 1;
$left = $this->left;
$right = $this->right;
if ($left != null && $right != null) {
$v += $left->check() + $right->check();
}
return $v;
}
}
function make(int $depth): Node {
$node = new Node();
if ($depth > 0) {
$depth -= 1;
$node->left = make($depth);
$node->right = make($depth);
}
return $node;
}