-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.sc
40 lines (31 loc) · 1.13 KB
/
day14.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
import common.loadPackets
val input = loadPackets(List("day14.txt"))
val (xmax, ymax) = (101, 103)
case class Point(x: Long, y: Long):
def plus(d: Point, steps: Long = 1): Point =
Point(x = Math.floorMod(x + d.x * steps, xmax), y = Math.floorMod(y + d.y * steps, ymax))
def quadrant: Option[Point] = Some(Point((x - xmax / 2).sign, (y - ymax / 2).sign))
.filter(_.x != 0).filter(_.y != 0)
case class Bot(p: Point, v: Point):
def posAt(t: Int): Point = p.plus(v, t)
case object Bots:
def parse(line: String): Bot = line match {
case s"p=${x},${y} v=${dx},${dy}" =>
Bot(Point(x.toInt, y.toInt), Point(dx.toInt, dy.toInt))
}
val bots = input.map(Bots.parse)
val part1 = bots.map(_.posAt(100))
.flatMap(_.quadrant).groupBy(x => x).values.map(_.size).product
def drawBotsAt(t: Int) = {
val positions = bots.map(_.posAt(t))
(0 until xmax).map(y => {
(0 until xmax).map(x => {
val p = Point(x, y)
if positions.contains(p) then '#' else ' '
}).mkString
}).mkString("\n")
}
val part2 = LazyList.from(0)
.map(drawBotsAt)
.indexWhere(_.contains("###############################"))
drawBotsAt(part2)