Skip to content

Commit e4df7b9

Browse files
committed
2022/Day 07
1 parent a634469 commit e4df7b9

File tree

5 files changed

+1080
-0
lines changed

5 files changed

+1080
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
🎄 --- Day 5: Supply Stacks ---
2727
🌟 Part 1: MQTPGLLDN (7.32ms)
2828
🌟 Part 2: LVZPSTTCZ (4.71ms)
29+
30+
🎄 --- Day 6: Tuning Trouble ---
31+
🌟 Part 1: 1080 (9.47ms)
32+
🌟 Part 2: 3645 (4.73ms)
33+
34+
🎄 --- Day 7: No Space Left On Device ---
35+
🌟 Part 1: 1582412 (11.92ms)
36+
🌟 Part 2: 3696336 (74.4us)
37+
2938
```
3039

3140
## Visualizations

src/main/kotlin/me/grison/aoc/Collections.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ fun <T> List<List<T>>.transpose(): List<List<T>> {
9494
.toList()
9595
}
9696

97+
fun <T> MutableList<T>.reset(item: T) : MutableList<T> {
98+
this.clear()
99+
this.add(item)
100+
return this
101+
}
102+
97103
/** Make an ArrayDeque representing this Collection. */
98104
fun <T> Collection<T>.deque() = ArrayDeque(this)
99105

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package me.grison.aoc.y2022
2+
3+
import arrow.syntax.function.memoize
4+
import me.grison.aoc.*
5+
6+
class Day07 : Day(7, 2022) {
7+
override fun title() = "No Space Left On Device"
8+
9+
override fun partOne() = disk().values.filter { it <= 100000 }.sum()
10+
11+
override fun partTwo() = (70000000 - disk().getValue("/<root>")).let { emptySpace ->
12+
disk().values.filter { emptySpace + it > 30000000 }.minOrNull()
13+
}
14+
15+
private val disk = {
16+
val directorySizes = mutableMapOf<String, Int>().withDefault { 0 }
17+
val currentPath = mutableListOf<String>()
18+
inputList
19+
.filter { !it.startsWith("dir ") }
20+
.map { ("$it #").words() }
21+
.forEach { (promptOrSize, commandOrName, cdPath) ->
22+
when (promptOrSize) {
23+
"$" -> when (commandOrName) {
24+
"cd" -> when (cdPath) {
25+
"/" -> currentPath.reset("<root>")
26+
".." -> currentPath.removeLast()
27+
else -> currentPath.append(cdPath)
28+
}
29+
}
30+
else -> currentPath.scan("") { acc, dir -> "$acc/$dir" }.forEach { fullPath ->
31+
directorySizes.increase(fullPath, promptOrSize.toInt())
32+
}
33+
}
34+
}
35+
directorySizes
36+
}.memoize()
37+
}

0 commit comments

Comments
 (0)