-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.scala
38 lines (28 loc) · 940 Bytes
/
day4.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
object Day4 {
def main(args: Array[String]): Unit = {
val convert = (i: Int) => {
i.toString.map(_.asDigit).toList
}
val isPair = (i: List[Int]) => {
i.zip(i.tail).exists((pair) => pair._1 == pair._2)
}
val isRaising = (i: List[Int]) => {
i.zip(i.tail).map((pair) => pair._1 <= pair._2).reduce(_ && _)
}
val isPassword = (i: Int) => {
convert.andThen(isPair)(i) && convert.andThen(isRaising)(i)
}
val input = 158126 to 624574
// First
println(input.map(isPassword).filter(_ == true).length)
// Second
val hasOnePair = (i: List[Int]) => {
val pairs = i.zip(i.tail).filter((pair) => pair._1 == pair._2)
pairs.groupBy(_._1).exists(_._2.length == 1)
}
val isPasswordSecond = (i: Int) => {
convert.andThen(hasOnePair)(i) && convert.andThen(isRaising)(i)
}
println(input.map(isPasswordSecond).filter(_ == true).length)
}
}