-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay03RucksackReorganization.kt
More file actions
31 lines (26 loc) · 995 Bytes
/
Day03RucksackReorganization.kt
File metadata and controls
31 lines (26 loc) · 995 Bytes
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
package adventofcode.year2022
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day03RucksackReorganization(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
private val rucksacks by lazy { input.lines() }
override fun partOne() =
rucksacks
.map { rucksack ->
listOf(
rucksack.substring(0 until rucksack.length / 2).toSet(),
rucksack.substring(rucksack.length / 2).toSet(),
)
}.flatMap { rucksack -> rucksack.reduce(Set<Char>::intersect) }
.sumOf { item -> item.toPriority() }
override fun partTwo() =
rucksacks
.chunked(3)
.map { group -> group.map(String::toSet) }
.flatMap { group -> group.reduce(Set<Char>::intersect) }
.sumOf { item -> item.toPriority() }
companion object {
private fun Char.toPriority() = (('a'..'z') + ('A'..'Z')).indexOf(this) + 1
}
}