-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.kt
170 lines (137 loc) · 5.12 KB
/
App.kt
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
package day07.solution1
// Original solution used to solve the AoC day 7 puzzle. Warts and all.
import common.Solution
typealias ParsedInput = List<String>
const val ROOT_PATH = "/"
fun pathfor(basepath: String, name: String): String {
if (name == ROOT_PATH) return ROOT_PATH
return if (basepath == ROOT_PATH) "$ROOT_PATH$name" else "$basepath/$name"
}
sealed class FSNode {
abstract val name: String;
abstract val path: String;
val absolutepath: String
get() = pathfor(path, name)
data class File(override var name: String, override val path: String, val size: Int): FSNode()
data class Dir(override val name: String, override val path: String, val items: Set<String> = setOf()): FSNode() {
fun addNode(n: FSNode): Dir {
return this.copy(items = this.items + n.name)
}
fun contains(n: FSNode): Boolean {
return n.name in this.items
}
fun contains(n: String): Boolean {
return n in this.items
}
}
}
typealias FSNodes = Map<String, FSNode>
fun buildFileSystemFromInput(input: List<String>): FSNodes {
val root = FSNode.Dir(name = ROOT_PATH, path = ROOT_PATH)
var fsnodes: FSNodes = mapOf<String, FSNode>(root.name to root)
var dirStack = listOf<String>()
var lines = input.toList()
while (lines.isNotEmpty()) {
val line = lines.first()
lines = lines.drop(1)
if (line[0] != '$') {
break
}
val parts = line.split(" ")
val (_, cmd) = parts
when (cmd) {
"cd" -> {
when (val arg = parts.last()) {
".." -> dirStack = dirStack.dropLast(1)
else -> dirStack += arg
}
}
"ls" -> {
var i = 0
while (i < lines.size && lines[i][0] != '$') {
val (arg, name) = lines[i].split(" ")
val pwdpath = root.name + dirStack.drop(1).joinToString("/")
val nodepath = pathfor(pwdpath, name)
if (nodepath !in fsnodes) {
val node = when (arg) {
"dir" -> FSNode.Dir(name = name, path = pwdpath)
else -> FSNode.File(name = name, path = pwdpath, size = arg.toInt())
}
fsnodes += (node.absolutepath to node)
val currDir = fsnodes[pwdpath]
when (currDir) {
is FSNode.Dir -> fsnodes += (pwdpath to currDir.addNode(node))
else -> throw error("current directory is not a directory $currDir")
}
}
i += 1
}
lines = lines.drop(i)
}
}
}
return fsnodes
}
fun FSNodes.nodeSizeFor(path: String, cache: MutableMap<String, Long> = mutableMapOf()): Long {
if (path in cache) {
return cache[path]!!
}
val size = when (val node = this[path]!!) {
is FSNode.Dir -> node.items.sumOf { this.nodeSizeFor(pathfor(path, it), cache) }
is FSNode.File -> node.size.toLong()
else -> throw error("Unknown node type")
}
cache[path] = size
return size
}
fun FSNodes.prettyPrintTree(nodepath: String = "/", indent: Int = 0) {
val node = this[nodepath]!!
print((1..indent).map { ' ' }.joinToString("") + "- ${node.name} ")
when (node) {
is FSNode.Dir -> {
println("(dir)")
node.items.forEach { nodename ->
this.prettyPrintTree("${if (nodepath == "/") "" else nodepath}/$nodename", indent + 4)
}
}
is FSNode.File -> {
println("(file, size = ${node.size})")
}
else -> throw error("Unknown node $nodepath")
}
}
object Day07 : Solution.LinedInput<ParsedInput>(day = 7) {
override fun parseInput(input: List<String>): ParsedInput {
return input
}
override fun part1(input: ParsedInput): Any {
var fsnodes = buildFileSystemFromInput(input)
val cache = mutableMapOf<String, Long>()
return fsnodes.values
.filterIsInstance<FSNode.Dir>()
.map { (it.absolutepath to fsnodes.nodeSizeFor(it.absolutepath, cache)) }
.filter { it.second <= 100000 }
.sumOf { it.second }
}
override fun part2(input: ParsedInput): Any {
var fsnodes = buildFileSystemFromInput(input)
val fsDiskSpace = 70000000
val requiredDiskSpace = 30000000
val usedSpace = fsnodes.nodeSizeFor(ROOT_PATH)
val avilableSpace = fsDiskSpace - usedSpace
val minSpaceToFree = requiredDiskSpace - avilableSpace
val cache = mutableMapOf<String, Long>()
return fsnodes.values
.filter { when (it) {
is FSNode.Dir -> true
else -> false
} }
.map { fsnodes.nodeSizeFor(it.absolutepath, cache) }
.sortedBy { it }
.dropWhile { it < minSpaceToFree }
.first()
}
}
fun main() {
Day07.solve(test = false)
}