generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2015D08.kt
31 lines (24 loc) · 1.12 KB
/
Y2015D08.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 aockt.y2015
import io.github.jadarma.aockt.core.Solution
/**
* **NOTE:** This solution assumes the input strings are valid (i.e. all escapes are correct, and surrounding quotes
* are intact). This is, after all, an analysis of Santa's digital list, which is code, and therefore I'll pretend an
* elf already ran it through a linter before deploying to production.
*/
object Y2015D08 : Solution {
private val escapedHex = Regex("""\\x[0-9a-f]{2}""")
private val escapedQuote = Regex("""\\"""")
private val escapedBackslash = Regex("""\\\\""")
private fun String.memorySize(): Int =
// The order is important!
replace(escapedHex, "!")
.replace(escapedQuote, "!")
.replace(escapedBackslash, "!")
.length - 2
private fun String.escaped(): String =
replace("""\""", """\\""")
.replace(""""""", """\"""")
.let { """"$it"""" }
override fun partOne(input: String) = input.lineSequence().sumOf { it.length - it.memorySize() }
override fun partTwo(input: String) = input.lineSequence().sumOf { it.escaped().length - it.length }
}