-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.go
239 lines (201 loc) · 4.46 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"fmt"
"sort"
"strconv"
"strings"
)
type AOC202207Node struct {
Name string
Size int
Child []*AOC202207Node
Parent *AOC202207Node
}
func (node *AOC202207Node) Path() string {
if node.Name == "" {
return ""
}
return node.Parent.Path() + "/" + node.Name
}
func (node *AOC202207Node) String() string {
rtn := []string{
fmt.Sprintf("%s: %d", node.Path(), node.GetSize()),
}
for _, child := range node.Child {
rtn = append(rtn, child.String())
}
return strings.Join(rtn, "\n")
}
func (node *AOC202207Node) FindChild(name string) (*AOC202207Node, error) {
for _, child := range node.Child {
if child.Name == name {
return child, nil
}
}
return nil, fmt.Errorf("node %s has no child %s", node.Name, name)
}
func (node *AOC202207Node) CreateDirectory(name string) {
node.Child = append(node.Child, &AOC202207Node{
Name: name,
Child: []*AOC202207Node{},
Parent: node,
})
}
func (node *AOC202207Node) CreateFile(name string, size int) {
node.Child = append(node.Child, &AOC202207Node{
Name: name,
Parent: node,
Size: size,
})
}
func (node *AOC202207Node) GetSize() int {
size := 0
for _, child := range node.Child {
if child.Child == nil {
size += child.Size
} else {
size += child.GetSize()
}
}
node.Size = size
return size
}
func (node *AOC202207Node) GetDirsizes() []int {
rtn := []int{}
rtn = append(rtn, node.Size)
for _, child := range node.Child {
if child.Child != nil {
rtn = append(rtn, child.GetDirsizes()...)
}
}
return rtn
}
type AOC202207FS struct {
Root *AOC202207Node
Position *AOC202207Node
}
func (fs *AOC202207FS) String() string {
return fs.Root.String()
}
func (fs *AOC202207FS) Size() int {
return fs.Root.GetSize()
}
func (fs *AOC202207FS) GetNode(path string) (*AOC202207Node, error) {
parts := strings.Split(path, "/")
if parts[0] != "" {
return nil, fmt.Errorf("can't find node without a full path")
}
curNode := fs.Root
for _, part := range parts[1:] {
next, err := curNode.FindChild(part)
if err != nil {
return nil, fmt.Errorf("%s has no child %s", curNode.Path(), part)
}
curNode = next
}
return curNode, nil
}
func (fs *AOC202207FS) GetDirsizes() []int {
return fs.Root.GetDirsizes()
}
func (fs *AOC202207FS) Populate(input string) error {
dirmode := false
for _, line := range strings.Split(input, "\n") {
if line == "$ ls" {
dirmode = true
continue
}
if line == "$ cd /" {
dirmode = false
fs.Position = fs.Root
continue
}
if line == "$ cd .." {
dirmode = false
fs.Position = fs.Position.Parent
continue
}
if line[:5] == "$ cd " {
dirmode = false
target := strings.Split(line, " ")[2]
child, err := fs.Position.FindChild(target)
if err != nil {
return fmt.Errorf("node %s: can't switch to child %s: %v", fs.Position.Name, target, err)
}
fs.Position = child
continue
}
if dirmode {
parts := strings.Split(line, " ")
if parts[0] == "dir" {
fs.Position.CreateDirectory(parts[1])
} else {
size, err := strconv.Atoi(parts[0])
if err != nil {
return fmt.Errorf("can't convert size of \"%s\" into an integer: %v", line, err)
}
fs.Position.CreateFile(parts[1], size)
}
continue
}
return fmt.Errorf("unexpected entry: %s", line)
}
fs.Size()
return nil
}
func AOC202207NewFS() (*AOC202207FS, error) {
fs := &AOC202207FS{
Root: &AOC202207Node{
Name: "",
Child: []*AOC202207Node{},
},
}
fs.Position = fs.Root
return fs, nil
}
func (fs *AOC202207FS) AOC2022071Helper() int {
sum := 0
for _, elem := range fs.GetDirsizes() {
if elem <= 100000 {
sum += elem
}
}
return sum
}
func AOC202207FSInit(input string) (*AOC202207FS, error) {
fs, err := AOC202207NewFS()
if err != nil {
return nil, fmt.Errorf("can't init new FS: %v", err)
}
fs.Populate(input)
return fs, nil
}
func AOC2022071(input string) (string, error) {
fs, err := AOC202207FSInit(input)
if err != nil {
return "", err
}
return fmt.Sprintf("%d", fs.AOC2022071Helper()), nil
}
func (fs *AOC202207FS) AOC2022072Helper() int {
space := 70000000
requiredFree := 30000000
currentFree := space - fs.Size()
delta := requiredFree - currentFree
dirsizes := fs.GetDirsizes()
sort.Ints(dirsizes)
for _, size := range dirsizes {
if size < delta {
continue
}
return size
}
return 0
}
func AOC2022072(input string) (string, error) {
fs, err := AOC202207FSInit(input)
if err != nil {
return "", err
}
return fmt.Sprintf("%d", fs.AOC2022072Helper()), nil
}