File tree Expand file tree Collapse file tree 5 files changed +1080
-0
lines changed
test/kotlin/me/grison/aoc/y2022 Expand file tree Collapse file tree 5 files changed +1080
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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. */
98104fun <T > Collection<T>.deque () = ArrayDeque (this )
99105
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments