generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2022D03.kt
29 lines (24 loc) · 913 Bytes
/
Y2022D03.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
package aockt.y2022
import io.github.jadarma.aockt.core.Solution
object Y2022D03 : Solution {
/** The priority value of a knapsack item type. */
private val Char.priority: Int
get() = when (this) {
in 'a'..'z' -> this - 'a' + 1
in 'A'..'Z' -> this - 'A' + 27
else -> error("Char '$this' is not a valid item.")
}
override fun partOne(input: String) =
input
.lineSequence()
.map { it.substring(0..it.length / 2) to it.substring(it.length / 2..it.lastIndex) }
.map { (a, b) -> a.first { it in b } }
.sumOf { it.priority }
override fun partTwo(input: String) =
input
.lineSequence()
.chunked(3)
.map { group -> group.sortedBy { it.length } }
.map { (a, b, c) -> a.first { it in b && it in c } }
.sumOf { it.priority }
}