Day 4 y2019

This commit is contained in:
Aly 2019-12-03 21:40:32 -08:00 committed by Anthony Cerruti
parent f412003d6f
commit 8c9f95fe2c
2 changed files with 41 additions and 0 deletions

View file

@ -0,0 +1,40 @@
package aoc.y2019
import aoc.Day
object Day04 extends Day {
import fastparse._, SingleLineWhitespace._
def parseNumber[_: P]: P[Int] = P(CharIn("0-9").rep(1).!).map(_.toInt)
def parseRange[_: P]: P[Range.Inclusive] = P(parseNumber ~ "-" ~ parseNumber).map {
case (a, b) => a to b
}
def validPassword(password: Int, range: Range, repetitionBounded: Boolean): Boolean = {
lazy val isSixDigits = password.toString.length == 6
lazy val isWithinRange = range.contains(password)
lazy val hasRepetition =
if(!repetitionBounded)
password.toString.toSeq.groupBy(identity).exists(_._2.length >= 2)
else
password.toString.toSeq.groupBy(identity).exists(_._2.length == 2)
lazy val isIncreasing = password.toString.toSeq.sorted == password.toString.toSeq
isSixDigits && isWithinRange && hasRepetition && isIncreasing
}
def numberValidPasswords(rangeString: String, repetitionBounded: Boolean): Int = {
val range = parse(rangeString, parseRange(_)).get.value
range.count(validPassword(_, range, repetitionBounded))
}
override def part1(input: String): String = {
numberValidPasswords(input, repetitionBounded = false).toString
}
override def part2(input: String): String = {
numberValidPasswords(input, repetitionBounded = true).toString
}
}

View file

@ -6,6 +6,7 @@ package object y2019 extends Year {
"1" -> Day01,
"2" -> Day02,
"3" -> Day03,
"4" -> Day04,
)
}