-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.kt
31 lines (23 loc) · 1.13 KB
/
Day01.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
package io.dmitrijs.aoc2023
class Day01(private val input: List<String>) {
fun puzzle1() = input.sumOf { it.calibrationValue }
fun puzzle2() = input.sumOf { str ->
var line = str
regexHead.find(line)?.let { line = line.replaceDigit(it) }
regexTail.find(line)?.let { line = line.replaceDigit(it) }
line.calibrationValue
}
private fun String.replaceDigit(match: MatchResult): String {
val group = match.groups[1]!!
return replaceRange(group.range, digitsMap.getValue(group.value))
}
private val String.calibrationValue get() =
first { it.isDigit() }.digitToInt() * 10 + last { it.isDigit() }.digitToInt()
companion object {
private val digitsAbc = listOf("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
private val digitsMap = digitsAbc.mapIndexed { index, word -> word to (index + 1).toString() }.toMap()
private val digitsBag = digitsAbc.joinToString("|", "(", ")")
private val regexHead = Regex("^[a-z]*?$digitsBag[a-z0-9]+$")
private val regexTail = Regex("^[a-z0-9]+$digitsBag[a-z]*?$")
}
}