-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day9.kt
32 lines (29 loc) · 1.11 KB
/
Day9.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 aoc2016.day09
fun decompressSequence(input: String, isPart2: Boolean = false): Long {
var sequence = StringBuilder(input)
var index = 0
var accumulator = 0L
while (index < sequence.length) {
if (sequence[index] == '(') {
val end = sequence.indexOf(')', index)
val marker = sequence.substring(index + 1, end).split("x")
val (charNo, times) = Pair(marker[0].toInt(), marker[1].toInt())
val charsToRepeat = sequence.substring(end + 1, end + 1 + charNo).repeat(times)
sequence.delete(end + 1, end + 1 + charNo)
sequence.insert(end + 1, charsToRepeat)
sequence.delete(index, end + 1)
if (!isPart2)
index += charsToRepeat.length
} else {
index = sequence.indexOf('(', index)
if (index == -1) break
if (sequence.length > 10000000) {
accumulator += index
sequence = StringBuilder(sequence.substring(index))
index = 0
}
}
}
accumulator += sequence.length
return accumulator
}