-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay02.scala
43 lines (34 loc) · 1.5 KB
/
Day02.scala
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
37
38
39
40
41
42
43
package com.lmat.adventofcode.year2020
import com.lmat.adventofcode.SimpleCommonPuzzle
import com.lmat.adventofcode.year2020.Day02Definitions._
import com.lmat.util.Files.readResource
object Day02Definitions {
case class Policy(char: Char, min: Int, max: Int)
}
object Day02 extends SimpleCommonPuzzle[List[(Policy, String)], Int, Int] {
override def parse(resource: String): List[(Policy, String)] =
readResource(resource).flatMap(parsePolicyRow).toList
def parsePolicyRow(row: String): Option[(Policy, String)] = {
val pattern = s"(\\d+)-(\\d+) (.): (.*)".r
row match {
case pattern(minRaw, maxRaw, charRaw, password) => for {
char <- charRaw.toCharArray.headOption
min <- minRaw.toIntOption
max <- maxRaw.toIntOption
} yield (Policy(char, min, max), password)
case _ => None
}
}
override def part1(passwords: List[(Policy, String)]): Int =
passwords.count { case (policy, pass) => isValid(policy)(pass) }
override def part2(passwords: List[(Policy, String)]): Int =
passwords.count { case (policy, pass) => isValid2(policy)(pass) }
def isValid(policy: Policy)(password: String): Boolean = {
val givenCharCount = password.count(_ == policy.char)
policy.min <= givenCharCount && givenCharCount <= policy.max
}
def isValid2(policy: Policy)(password: String): Boolean = {
val relevantChars = List(policy.min, policy.max).map(_ - 1).flatMap(password.lift)
relevantChars.contains(policy.char) && relevantChars.toSet.size != 1
}
}