-
Notifications
You must be signed in to change notification settings - Fork 0
/
07.go
79 lines (73 loc) · 1.42 KB
/
07.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"bufio"
"fmt"
"os"
)
type file struct {
name string
size int
}
type dir struct {
name string
parent *dir
files []file
dirs []*dir
}
func (d *dir) size() int {
result := 0
for _, file := range d.files {
result += file.size
}
for _, dir := range d.dirs {
result += dir.size()
}
return result
}
func (d *dir) forEach(f func(d *dir)) {
for _, dir := range d.dirs {
dir.forEach(f)
}
f(d)
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
root := dir{name: "/", files: []file{}, dirs: []*dir{}}
current := &root
for scanner.Scan() {
line := scanner.Text()
if line[:4] == "$ ls" {
// skip
} else if line[:5] == "$ cd " {
switch line[5:] {
case "/":
current = &root
case "..":
current = current.parent
default:
for _, dir := range current.dirs {
if dir.name == line[5:] {
current = dir
}
}
}
} else if line[:3] == "dir" {
current.dirs = append(current.dirs, &dir{name: line[4:], files: []file{}, dirs: []*dir{}, parent: current})
} else {
var file file
fmt.Sscanf(line, "%d %s", &file.size, &file.name)
current.files = append(current.files, file)
}
}
total, smallest, rootSize := 0, 10000000000, root.size()
root.forEach(func(d *dir) {
size := d.size()
if size <= 100000 {
total += size
}
if size >= 30000000-(70000000-rootSize) && size < smallest {
smallest = size
}
})
fmt.Println(total, smallest)
}