-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.kt
19 lines (15 loc) · 1.1 KB
/
Day2.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package day02
import util.readFile
class Day2(private val inputFile: String) {
private val lines: List<String> = readFile(inputFile)
private val lineRegex = """(\d+)-(\d+) ([a-z]):\s([a-z]+)""".toRegex()
fun part1(): Int = countValidPasswords(::validatorPart1)
fun part2(): Int = countValidPasswords(::validatorPart2)
fun countValidPasswords(validator: (Int, Int, String, String) -> Boolean): Int = lines.count { checkPassword(it, validator) }
fun validatorPart1(minOccurs: Int, maxOccurs: Int, letter: String, password: String): Boolean = """^([^${letter}]*$letter[^$letter]*){$minOccurs,$maxOccurs}${'$'}""".toRegex().matches(password)
fun validatorPart2(pos1: Int, pos2: Int, letter: String, password: String): Boolean = password[pos1 - 1].toString().equals(letter).xor(password[pos2 - 1].toString().equals(letter))
fun checkPassword(passwordLine: String, validator: (Int, Int, String, String) -> Boolean): Boolean {
val (num1, num2, letter, password) = lineRegex.find(passwordLine)!!.destructured
return validator(num1.toInt(), num2.toInt(), letter, password)
}
}