Day02
This commit is contained in:
parent
dc4160ca73
commit
ea41ce0e24
29
days/src/main/kotlin/Day02.kt
Normal file
29
days/src/main/kotlin/Day02.kt
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package be.vandewalleh.aoc.days
|
||||||
|
|
||||||
|
import be.vandewalleh.aoc.utils.input.Day
|
||||||
|
import be.vandewalleh.aoc.utils.input.Input
|
||||||
|
import be.vandewalleh.aoc.utils.input.Lines
|
||||||
|
import be.vandewalleh.aoc.utils.input.createDay
|
||||||
|
|
||||||
|
data class PasswordEntry(val range: IntRange, val letter: Char, val password: String)
|
||||||
|
|
||||||
|
@Day(2)
|
||||||
|
class Day02(@Lines input: Input<List<String>>) {
|
||||||
|
private val regex = "^(\\d+)-(\\d+) ([a-z]): (.*)$".toRegex()
|
||||||
|
|
||||||
|
private val passwords = input.value.map {
|
||||||
|
val (_, min, max, letter, password) = regex.find(it)!!.groupValues
|
||||||
|
PasswordEntry(min.toInt()..max.toInt(), letter[0], password)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part1() = passwords.count { it.password.count { char -> char == it.letter } in it.range }
|
||||||
|
|
||||||
|
fun part2() = passwords.count { (range, letter, pwd) ->
|
||||||
|
arrayOf(pwd[range.first -1], pwd[range.last - 1]).filter { it == letter }.size == 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun main() = with(createDay<Day02>()) {
|
||||||
|
println(part1())
|
||||||
|
println(part2())
|
||||||
|
}
|
||||||
1000
days/src/main/resources/day02.txt
Normal file
1000
days/src/main/resources/day02.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user