-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01Trebuchet.kt
More file actions
41 lines (35 loc) · 1.11 KB
/
Day01Trebuchet.kt
File metadata and controls
41 lines (35 loc) · 1.11 KB
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
36
37
38
39
40
41
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)
}
}