-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02PasswordPhilosophy.kt
36 lines (28 loc) · 1.19 KB
/
Day02PasswordPhilosophy.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 adventofcode.year2020
import adventofcode.Puzzle
import adventofcode.PuzzleInput
class Day02PasswordPhilosophy(customInput: PuzzleInput? = null) : Puzzle(customInput) {
override fun partOne() =
input
.lines()
.count {
val matchResults = PASSWORD_REGEX.find(it)!!
val (min, max, char, password) = matchResults.destructured
val charCount = password.toCharArray().count { it.toString() == char }
charCount >= min.toInt() && charCount <= max.toInt()
}
override fun partTwo() =
input
.lines()
.count {
val matchResults = PASSWORD_REGEX.find(it)!!
val (pos1s, pos2s, char, password) = matchResults.destructured
val pos1 = pos1s.toInt() - 1
val pos2 = pos2s.toInt() - 1
((password[pos1].toString() == char) && (password[pos2].toString() != char)) ||
((password[pos1].toString() != char) && (password[pos2].toString() == char))
}
companion object {
private val PASSWORD_REGEX = """([0-9]*)-([0-9]*) ([a-z]): (.*)""".toRegex()
}
}