generated from Jadarma/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathY2015D05.kt
36 lines (29 loc) · 1.26 KB
/
Y2015D05.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
36
package aockt.y2015
import io.github.jadarma.aockt.core.Solution
object Y2015D05 : Solution {
// Validate this first so you can use a simple '.' for any character instead of [a-z] everywhere.
// Technically not needed since input is clean, but we should be thorough ;)
private val onlyLetters = Regex("""^[a-z]*$""")
// Rules for Part One
private val atLeastThreeVowels = Regex("""([aeiou].*){3,}""")
private val doubledLetter = Regex("""(.)\1""")
private val containsNaughtyString = Regex("""ab|cd|pq|xy""")
// Rules for Part Two
private val doublePairs = Regex("""(..).*\1""")
private val sandwichedLetter = Regex("""(.).\1""")
override fun partOne(input: String) =
input
.lineSequence()
.filter { onlyLetters.matches(it) }
.filter { atLeastThreeVowels.containsMatchIn(it) }
.filter { doubledLetter.containsMatchIn(it) }
.filterNot { containsNaughtyString.containsMatchIn(it) }
.count()
override fun partTwo(input: String) =
input
.lineSequence()
.filter { onlyLetters.matches(it) }
.filter { doublePairs.containsMatchIn(it) }
.filter { sandwichedLetter.containsMatchIn(it) }
.count()
}