-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.scala
34 lines (27 loc) · 934 Bytes
/
day2.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
package adventofcode
import scala.io.Source.*
@main def day2() =
val inputPath = helpers.InputDir + "2.txt"
val reports: Seq[Array[Int]] = scala.io.Source.fromFile("./inputs/2.txt", "UTF-8")
.getLines.toSeq
.map(_.split(" "))
.map(_.map(_.toInt))
//println(reports.length)
//This won't filter out if window.head == window.last
val reportsWithOnlyIncOrDec: Seq[Array[Int]] =
reports.filter(report =>
report
.sliding(2)
.map(window => window.head > window.last).toSeq
.distinct.length == 1
)
val reportsWithGoodIncOrDec: Seq[Array[Int]] =
reports.filter(report =>
report
.sliding(2)
.map(window => (window.head - window.last).abs >= 1 && (window.head - window.last).abs <= 3)
.toSeq
.contains(false) != true
)
val goodReports = reportsWithGoodIncOrDec.intersect(reportsWithOnlyIncOrDec)
println("Part 1: " + goodReports.length)