-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay07NoSpaceLeftOnDevice.kt
50 lines (42 loc) · 1.72 KB
/
Day07NoSpaceLeftOnDevice.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
package adventofcode.year2022
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day07NoSpaceLeftOnDevice(customInput: PuzzleInput? = null) : Puzzle(customInput) {
private val files by lazy {
input.lines().fold(emptyList<String>() to emptySet<File>()) { (currentPath, files), line ->
val (size, name) = line.split(" ")
when {
line == "$ cd .." -> currentPath.dropLast(1) to files
line.startsWith("$ cd ") -> currentPath + listOf(line.split(" ").last()) to files
line.startsWith("dir") -> currentPath to files + File(name, currentPath.joinToString("/"), 0)
line.first().isDigit() -> currentPath to files + File(name, currentPath.joinToString("/"), size.toInt())
else -> currentPath to files
}
}
.second
}
private val directories by lazy {
files
.map(File::path)
.toSet()
.map { directory -> directory to files.filter { file -> file.path.startsWith(directory) }.sumOf { file -> file.size } }
}
override fun partOne() =
directories
.filter { (_, size) -> size <= 100000 }
.sumOf { (_, size) -> size }
override fun partTwo() =
directories
.filter { (_, size) -> size >= REQUIRED_SPACE - (TOTAL_DISK_SPACE - directories.maxOf { (_, size) -> size }) }
.minBy { (_, size) -> size }
.second
companion object {
private const val TOTAL_DISK_SPACE = 70000000
private const val REQUIRED_SPACE = 30000000
private data class File(
val name: String,
val path: String,
val size: Int,
)
}
}