-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day01Trebuchet.kt
35 lines (29 loc) · 1020 Bytes
/
Day01Trebuchet.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
34
35
package adventofcode.year2023
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day01Trebuchet(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override val name = "Trebuchet?!"
override fun partOne() = input
.lines()
.sumOfFirstAndLastDigits()
override fun partTwo() = input
.lines()
.map { line -> digitMap.entries.fold(line) { acc, (word, digit) -> acc.replace(word, "$word$digit$word") } }
.sumOfFirstAndLastDigits()
companion object {
private val digitMap = mapOf(
"one" to 1,
"two" to 2,
"three" to 3,
"four" to 4,
"five" to 5,
"six" to 6,
"seven" to 7,
"eight" to 8,
"nine" to 9
)
private fun List<String>.sumOfFirstAndLastDigits() = map { line -> line.filter(Char::isDigit) }
.map { line -> String(charArrayOf(line.first(), line.last())) }
.sumOf(String::toInt)
}
}