Skip to content

Commit a6da1f3

Browse files
committed
2022/Day 05
1 parent f0fdba2 commit a6da1f3

File tree

6 files changed

+553
-1
lines changed

6 files changed

+553
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@
1818
🎄 --- Day 03: Rucksack Reorganization ---
1919
🌟 Part 1: 7553 (4.26ms)
2020
🌟 Part 2: 2758 (1.83ms)
21-
21+
22+
🎄 --- Day 4: Camp Cleanup ---
23+
🌟 Part 1: 450 (3.52ms)
24+
🌟 Part 2: 837 (2.5ms)
25+
26+
🎄 --- Day 5: Supply Stacks ---
27+
🌟 Part 1: MQTPGLLDN (7.32ms)
28+
🌟 Part 2: LVZPSTTCZ (4.71ms)
2229
```
2330

2431
## Visualizations

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,6 @@ fun <T> List<List<T>>.iterate(dimensions: Pair<Int, Int> = dimensions()): Sequen
182182
fun <T> List<Set<T>>.intersectAll() = this.reduce { a, b -> a.intersect(b) }
183183
fun <T> List<Set<T>>.unionAll() = this.reduce { a, b -> a.union(b) }
184184
fun List<String>.mapSet() = this.map { it.toSet() }
185+
186+
fun <T> List<T>.rejectAll(elem: T) = this.filter { it != elem }
187+
fun <T> List<T>.mutable() = this.toMutableList()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ fun String.split(at: Int) = listOf(this.take(at), this.drop(at))
147147

148148
fun String.inTwo() = this.split(this.length / 2)
149149

150+
fun String.remove(r: Regex) = this.replace(r, "")
151+
150152
fun foo() {
151153
"".replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
152154
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.grison.aoc.y2022
2+
3+
import me.grison.aoc.*
4+
5+
class Day05 : Day(5, 2022) {
6+
override fun title() = "Supply Stacks"
7+
8+
override fun partOne() = solve { it.reversed() }
9+
10+
override fun partTwo() = solve { it }
11+
12+
fun solve(mixer: (Iterable<String>) -> Iterable<String>): String {
13+
val (initial, moves) = inputGroups
14+
val stacks = initial.lines().butLast()
15+
.map { it.replace(" ", "[.]").remove("[^\\w.]+".regex()).list() }.transpose()
16+
.map { it.rejectAll(".") }.mutable()
17+
18+
moves.lines().forEach {
19+
val (amount, from, to) = it.allInts()
20+
stacks[to - 1] = mixer(stacks[from - 1].take(amount)) + stacks[to - 1]
21+
stacks[from - 1] = stacks[from - 1].drop(amount)
22+
}
23+
24+
return stacks.map { it[0] }.join()
25+
}
26+
}

0 commit comments

Comments
 (0)