-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day05.scala
25 lines (19 loc) · 907 Bytes
/
Day05.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
package AdventOfCode2021
object Day05:
case class Line(x1: Int, y1: Int, x2: Int, y2: Int):
def isOrthogonal: Boolean = x1 == x2 || y1 == y2
def points: Seq[(Int, Int)] =
val xs = x1 to x2 by (if x1 < x2 then 1 else -1)
val ys = y1 to y2 by (if y1 < y2 then 1 else -1)
xs.zipAll(ys, x1, y1)
def parse(input: Seq[String]): Seq[Line] = input.map { line =>
val Array(x1, y1, x2, y2) = line.split("\\D+").map(_.toInt)
Line(x1, y1, x2, y2)
}
def overlap(lines: Seq[Line]): Int = lines.flatMap(_.points).groupBy(identity).count(_._2.length > 1)
def part1(input: Seq[String]): Int = overlap(parse(input).filter(_.isOrthogonal))
def part2(input: Seq[String]): Int = overlap(parse(input))
def main(args: Array[String]): Unit =
val data = io.Source.fromResource("AdventOfCode2021/Day05.txt").getLines().toSeq
println(part1(data))
println(part2(data))