-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
235 lines (188 loc) · 5.42 KB
/
main.go
File metadata and controls
235 lines (188 loc) · 5.42 KB
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
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
"strings"
)
// CdCommand represents a shell command that ran cd.
type CdCommand struct {
// The directory argument.
directory string
}
// LsCommand represents a shell command that ran ls.
type LsCommand struct {
// The output directories.
dirs []LsDir
// The output files.
files []LsFile
}
// LsDir represents an output of an LsCommand that is a directory.
type LsDir struct {
name string
}
// LsFile represents an output of an LsCommand that is a file.
type LsFile struct {
name string
size int
}
// Command represents a command.
type Command interface {
// Run runs the command given the current working directory. It should return the new working directory.
Run(cwd *Directory) *Directory
}
func parse(input string) []Command {
lines := strings.Split(strings.TrimSpace(input), "\n")
var commands []Command
for i := 0; i < len(lines); i++ {
line := lines[i]
if _, dir, ok := strings.Cut(line, "$ cd "); ok {
commands = append(commands, CdCommand{
directory: dir,
})
} else if strings.Index(line, "$ ls") == 0 {
var lsInstr LsCommand
for _, lsLine := range lines[i+1:] {
if strings.Index(lsLine, "$ ") == 0 {
break
}
if _, dir, ok := strings.Cut(lsLine, "dir "); ok {
lsInstr.dirs = append(lsInstr.dirs, LsDir{
name: dir,
})
} else {
lsInstr.files = append(lsInstr.files, LsFile{
name: dir,
size: lib.Int(lsLine),
})
}
i += 1
}
commands = append(commands, lsInstr)
} else {
panic(line)
}
}
return commands
}
// File represents a file in a Directory.
type File struct {
name string
size int
}
// Size returns the file's size.
func (f File) Size() int {
return f.size
}
// Directory represents a directory.
type Directory struct {
name string
files []File
subdirectories []*Directory
parent *Directory
// Only computed after a call to Size.
size int
}
// Size returns the directory's size, which is the sum of all file sizes and subdirectory sizes.
func (d *Directory) Size() int {
// If already computed and cached, return that.
if d.size != 0 {
return d.size
}
totalFileSize := lib.SumSlice(lib.Map(d.files, File.Size))
totalSubdirectorySize := lib.SumSlice(lib.Map(d.subdirectories, (*Directory).Size))
size := totalFileSize + totalSubdirectorySize
d.size = size
return size
}
// Run returns the new working directory based on the CdCommand's argument.
func (c CdCommand) Run(cwd *Directory) *Directory {
switch c.directory {
case "..":
return cwd.parent
case "/":
return cwd // assume '/' only happens when the cwd is already root
default:
// Note: as is present in the input but not explicitly stated,
// this assumes the directory appeared in an ls before a cd into it
//
// To not make this assumption, the cd should always create a new subdirectory and add it to cwd
// And then the subdirectory part of LsCommand.Run should be removed/dirs not even parsed for ls output
//
// I like how this is organized better, though
subdir := lib.Filter(cwd.subdirectories, func(d *Directory) bool {
return d.name == c.directory
})[0]
return subdir
}
}
// Run adds the directories and files seen by the ls command to the current working directory.
func (l LsCommand) Run(cwd *Directory) *Directory {
for _, file := range l.files {
cwd.files = append(cwd.files, File{
name: file.name,
size: file.size,
})
}
for _, dir := range l.dirs {
cwd.subdirectories = append(cwd.subdirectories, &Directory{
name: dir.name,
parent: cwd,
})
}
return cwd
}
// buildDirectoryTree builds a directory tree based on the commands and returns the root of the tree.
func buildDirectoryTree(commands []Command) *Directory {
root := &Directory{
name: "/",
}
cwd := root
for _, command := range commands {
cwd = command.Run(cwd)
}
return root
}
// smallestDirectoryToDelete returns the size of the smallest directory rooted at d that when deleted frees at least
// spaceNeeded space.
func smallestDirectoryToDelete(d *Directory, spaceNeeded int) int {
smallest := -1
for _, subdir := range d.subdirectories {
dirSize := smallestDirectoryToDelete(subdir, spaceNeeded)
// This subdirectory isn't big enough
if dirSize == -1 {
continue
}
if smallest == -1 || dirSize < smallest {
smallest = dirSize
}
}
// If a subdirectory can be deleted to free enough space, this directory would only add more space and be larger
// so there's no need to check it.
if smallest != -1 {
return smallest
}
// Otherwise, if this directory is big enough, it must be the smallest, so return it
if d.Size() >= spaceNeeded {
return d.Size()
}
// Otherwise return -1 to indicate no directory was big enough
return -1
}
const (
diskSpaceAvailable = 70000000
diskSpaceForUpdate = 30000000
)
func solve(commands []Command) int {
root := buildDirectoryTree(commands)
totalSpaceUsed := root.Size()
totalSpaceRemaining := diskSpaceAvailable - totalSpaceUsed
totalSpaceNeeded := diskSpaceForUpdate - totalSpaceRemaining
return smallestDirectoryToDelete(root, totalSpaceNeeded)
}
func main() {
solver := lib.Solver[[]Command, int]{
ParseF: parse,
SolveF: solve,
}
solver.Expect("$ cd /\n$ ls\ndir a\n14848514 b.txt\n8504156 c.dat\ndir d\n$ cd a\n$ ls\ndir e\n29116 f\n2557 g\n62596 h.lst\n$ cd e\n$ ls\n584 i\n$ cd ..\n$ cd ..\n$ cd d\n$ ls\n4060174 j\n8033020 d.log\n5626152 d.ext\n7214296 k", 24933642)
solver.Verify(214171)
}