-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.sc
46 lines (34 loc) · 1.16 KB
/
day05.sc
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
44
45
46
// scala 2.13.6
import ammonite.ops._
import $file.runner
case class Point(x: Int, y: Int)
runner.exec[Int]("day05") { (fileName, assert) =>
val entries = read.lines(pwd / fileName).toList
val lines = entries.map { case s"$x1,$y1 -> $x2,$y2" =>
(Point(x1.toInt, y1.toInt), Point(x2.toInt, y2.toInt))
}
val (straight, diagonal) = lines
.partition {
case (p1, p2) if p1.x == p2.x => true
case (p1, p2) if p1.y == p2.y => true
case _ => false
}
def range(p1: Int, p2: Int) = (p1 to p2 by (if (p1 > p2) -1 else 1))
val points =
straight.flatMap { case (p1, p2) =>
for {
x <- range(p1.x, p2.x)
y <- range(p1.y, p2.y)
} yield Point(x, y)
}
def overlaps(points: List[Point]) =
points.groupBy(identity).collect { case (point, coll) if coll.size > 1 => coll.size }
assert("straight overlaps", 5, 6666)(overlaps(points).size)
val pointsDiagonal =
diagonal.flatMap { case (p1, p2) =>
for {
(x, y) <- range(p1.x, p2.x).zip(range(p1.y, p2.y))
} yield Point(x, y)
}
assert("overlaps diagonal", 12, 19081)(overlaps(points ++ pointsDiagonal).size)
}