-
Notifications
You must be signed in to change notification settings - Fork 0
/
solutiona.php
62 lines (50 loc) · 1.42 KB
/
solutiona.php
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
<?php
class Node {
public function __construct($parent) {
$this->parent = $parent;
$this->children = array();
$this->size = 0;
}
public function count_directory_sizes() {
foreach ($this->children as $k => $v) {
$v->count_directory_sizes();
$this->size += $v->size;
}
}
public function find_small_directories() {
$size = $this->is_directory() && $this->size <= 100000 ? $this->size : 0;
foreach ($this->children as $k => $v) {
$size += $v->find_small_directories();
}
return $size;
}
private function is_directory() {
return count($this->children) > 0;
}
}
$root = new Node(NULL);
$node = $root;
while ($line = fgets(STDIN)) {
$split = array_map("trim", explode(" ", $line));
if ($split[0] == "$" && $split[1] == "cd") {
if ($split[2] == "/") {
$node = $root;
} else if ($split[2] == "..") {
$node = $node->parent == NULL ? $root : $node->parent;
} else {
if (!array_key_exists($split[2], $node->children)) {
$node->children[$split[2]] = new Node($node);
}
$node = $node->children[$split[2]];
}
} else if ($split[0] != "$") {
if (!array_key_exists($split[1], $node->children)) {
$node->children[$split[1]] = new Node($node);
}
if ($split[0] != "dir") {
$node->children[$split[1]]->size = $split[0];
}
}
}
$root->count_directory_sizes();
echo "{$root->find_small_directories()}\n";