-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.go
98 lines (84 loc) · 1.86 KB
/
day07.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"strconv"
"strings"
)
type file struct {
name string
size int
}
type folder struct {
name string
subs []*folder
files []file
parent *folder
size int
}
func calcDirectorySize(lines []string) int {
var size int = 0
root := readFileSystem(lines)
sumFolders(root, &size)
return size
}
func findDeleteFolder(lines []string) int {
root := readFileSystem(lines)
spaceNeeded := 30000000 - (70000000 - root.size)
smallestFolder := root
findSmallestFolder(root, spaceNeeded, &smallestFolder)
return smallestFolder.size
}
func findSmallestFolder(current *folder, spaceNeeded int, smallestFolder **folder) {
if current.size > spaceNeeded && current.size < (*(smallestFolder)).size {
(*smallestFolder) = current
}
for _, f := range current.subs {
findSmallestFolder(f, spaceNeeded, smallestFolder)
}
}
func sumFolders(current *folder, sum *int) {
if current.size <= 100000 {
*sum += current.size
}
for _, f := range current.subs {
sumFolders(f, sum)
}
}
func readFileSystem(lines []string) *folder {
var root *folder = nil
var current *folder = nil
for _, v := range lines {
if strings.Contains(v, "$ cd") {
parts := strings.Split(v, " ")
path := parts[2]
switch path {
case "..":
current = current.parent
default:
if current == nil {
new := folder{name: path}
current = &new
root = &new
} else {
new := folder{name: path, parent: current}
current.subs = append(current.subs, &new)
current = &new
}
}
} else {
parts := strings.Split(v, " ")
i, err := strconv.Atoi(parts[0])
if err == nil {
file := file{name: parts[1], size: i}
current.files = append(current.files, file)
publishSize(current, i)
}
}
}
return root
}
func publishSize(current *folder, i int) {
current.size += i
if current.parent != nil {
publishSize(current.parent, i)
}
}