-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day1.kt
33 lines (28 loc) · 1 KB
/
Day1.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
33
package com.github.ephemient.aoc2023
class Day1(input: String) {
private val lines = input.lines()
fun part1() = solve(digits)
fun part2() = solve(extendedDigits)
private fun solve(values: Collection<IndexedValue<String>>): Int = lines.sumOf { line ->
val subset = values.filter { it.value in line }
if (subset.isNotEmpty()) {
10 * subset.minBy { line.indexOf(it.value) }.index + subset.maxBy { line.lastIndexOf(it.value) }.index
} else {
0
}
}
companion object {
private val digits = List(10) { IndexedValue(it, it.toString()) }
private val extendedDigits = digits + listOf(
IndexedValue(1, "one"),
IndexedValue(2, "two"),
IndexedValue(3, "three"),
IndexedValue(4, "four"),
IndexedValue(5, "five"),
IndexedValue(6, "six"),
IndexedValue(7, "seven"),
IndexedValue(8, "eight"),
IndexedValue(9, "nine"),
)
}
}