-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10ElvesLookElvesSay.kt
More file actions
40 lines (33 loc) · 1.12 KB
/
Day10ElvesLookElvesSay.kt
File metadata and controls
40 lines (33 loc) · 1.12 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
package adventofcode.year2015
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day10ElvesLookElvesSay(
customInput: PuzzleInput? = null,
) : Puzzle(customInput) {
override val name = "Elves Look, Elves Say"
override fun partOne() =
generateSequence(input) { it.elfLookElfSay() }
.drop(1)
.take(40)
.last()
.length
override fun partTwo() =
generateSequence(input) { it.elfLookElfSay() }
.drop(1)
.take(50)
.last()
.length
companion object {
private fun String.elfLookElfSay(): String {
val digitGroups = mutableListOf<Pair<Int, Int>>()
map { it.toString().toInt() }.forEach { digit ->
if (digitGroups.isNotEmpty() && digitGroups.last().second == digit) {
digitGroups[digitGroups.size - 1] = digitGroups.last().first + 1 to digit
} else {
digitGroups.add(1 to digit)
}
}
return digitGroups.flatMap { it.toList() }.joinToString("")
}
}
}