-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day03.kt
32 lines (26 loc) · 992 Bytes
/
Day03.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
class Day03 : Day(3, 2022, "Rucksack Reorganization") {
override fun part1() =
input.asSequence()
.map { rucksack -> rucksack.chunked(rucksack.length / 2) }
.sumOf { compartments -> compartments.singleCommonItem().priority }
override fun part2() =
input.asSequence()
.chunked(3)
.sumOf { rucksacks -> rucksacks.singleCommonItem().priority }
private fun Iterable<CharSequence>.singleCommonItem() =
asSequence().map(CharSequence::toSet).reduce(Set<Char>::intersect).single()
private val Char.priority get() = prioritiesInOrder.indexOf(this)
private val prioritiesInOrder = listOf('-') + ('a'..'z') + ('A'..'Z')
}
fun main() {
solve<Day03> {
"""
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
""".trimIndent()(157, 70)
}
}