-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.go
155 lines (121 loc) Β· 3.24 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
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
package day07
import (
"math"
"strconv"
"strings"
)
type File struct {
name string
size int
}
type ContentTree struct {
files map[string]File
directories map[string]*Directory
}
type Directory struct {
name string
size int
parent *Directory
contents ContentTree
}
func (dir *Directory) addFile(file File) {
dir.contents.files[file.name] = file
}
func (dir *Directory) addDirectory(directory *Directory) {
dir.contents.directories[directory.name] = directory
}
func calculateDirectorySize(directory *Directory) {
for _, dir := range directory.contents.directories {
calculateDirectorySize(dir)
directory.size += dir.size
}
for _, file := range directory.contents.files {
directory.size += file.size
}
}
func buildFilesystem(terminalOutput []string, rootDirectory *Directory) {
currentDirectory := rootDirectory
for _, line := range terminalOutput {
parts := strings.Split(line, " ")
if parts[0] == "$" {
if parts[1] == "ls" {
continue
}
// NOTE: `cd` command
if parts[2] == "/" {
currentDirectory = rootDirectory
} else if parts[2] == ".." {
currentDirectory = currentDirectory.parent
} else {
currentDirectory = currentDirectory.contents.directories[parts[2]]
}
} else {
if parts[0] == "dir" {
newDirectory := Directory{
parts[1],
0,
currentDirectory,
ContentTree{make(map[string]File), make(map[string]*Directory)},
}
currentDirectory.addDirectory(&newDirectory)
} else {
numericSize, err := strconv.Atoi(parts[0])
if err != nil {
panic("Error converting file size to int")
}
newFile := File{parts[1], numericSize}
currentDirectory.addFile(newFile)
}
}
}
calculateDirectorySize(rootDirectory)
}
const DELETION_SIZE_THRESHOLD = 100000
func sumDeletionCandidates(directory *Directory, sum int) int {
for _, dir := range directory.contents.directories {
if dir.size <= DELETION_SIZE_THRESHOLD {
sum += dir.size
}
sum = sumDeletionCandidates(dir, sum)
}
return sum
}
func part1(input string) int {
lines := strings.Split(input, "\n")
rootDirectory := Directory{
"/",
0,
nil,
ContentTree{make(map[string]File), make(map[string]*Directory)},
}
buildFilesystem(lines, &rootDirectory)
return sumDeletionCandidates(&rootDirectory, 0)
}
const TOTAL_DISK_SPACE = 70000000
const UPDATE_SIZE = 30000000
func part2(input string) int {
lines := strings.Split(input, "\n")
rootDirectory := Directory{
"/",
0,
nil,
ContentTree{make(map[string]File), make(map[string]*Directory)},
}
buildFilesystem(lines, &rootDirectory)
freeSpace := TOTAL_DISK_SPACE - rootDirectory.size
neededSpace := UPDATE_SIZE - freeSpace
closestDirToNeededSpace := math.MaxInt
var findClosestDirToNeededSpace func(*Directory)
findClosestDirToNeededSpace = func(directory *Directory) {
for _, dir := range directory.contents.directories {
previousExtraFreeSpace := closestDirToNeededSpace - neededSpace
extraFreeSpaceAfterDeletion := dir.size - neededSpace
if extraFreeSpaceAfterDeletion > 0 && extraFreeSpaceAfterDeletion < previousExtraFreeSpace {
closestDirToNeededSpace = dir.size
}
findClosestDirToNeededSpace(dir)
}
}
findClosestDirToNeededSpace(&rootDirectory)
return closestDirToNeededSpace
}