-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path07.kt
33 lines (32 loc) · 1.12 KB
/
07.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
data class Tree(val name: String, var parent: Tree?) {
var size: Int = 0
val children: MutableList<Tree> = mutableListOf()
}
fun main() {
val shell = generateSequence(::readlnOrNull).toList()
.map { it.replace("$ ", "") }
.filter { it != "ls" }
val root = Tree("/", null)
var current = root
for (line in shell) {
var (command, arg) = line.split(" ")
when (command) {
"cd" -> when (arg) {
"/" -> current = root
".." -> current = current.parent!!
else -> current = current.children.filter { it.name == arg }.first()
}
"dir" -> current.children.add(Tree(arg, current))
else -> current.size += command.toInt()
}
}
val sizes: MutableList<Int> = mutableListOf()
fun recursiveSizes(tree: Tree): Int {
sizes.add(tree.size + tree.children.sumOf(::recursiveSizes) )
return sizes.last()
}
recursiveSizes(root)
println(sizes.filter { it <= 100000 }.sum())
val needed = 30000000 - (70000000 - sizes.max())
println(sizes.filter { it >= needed }.min())
}