-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.sc
27 lines (21 loc) · 946 Bytes
/
day08.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
import common.loadPackets
val input = loadPackets(List("day08.txt"))
val ys = input.indices
val xs = input.head.indices
case class Point(x: Int, y: Int):
def plus(d: Point, steps: Int = 1): Point = Point(x = x + d.x * steps, y = y + d.y * steps)
def charAt: Char = input(y).charAt(x)
val points = for
y <- ys
x <- xs
yield Point(x, y)
val antennas = points.filter(_.charAt != '.')
val antennasByChar = antennas.groupBy(_.charAt)
def isAntinode(candidate: Point, antenna: Point, factors: Seq[Int] = Seq(1)): Boolean =
antennasByChar(antenna.charAt)
.filter(_ != antenna)
.map(other => antenna.plus(other, -1))
.flatMap(diff => factors.map(factor => antenna.plus(diff, factor)))
.contains(candidate)
val part1 = points.count(candidate => antennas.exists(antenna => isAntinode(candidate, antenna)))
val part2 = points.count(candidate => antennas.exists(antenna => isAntinode(candidate, antenna, -xs.last to xs.last)))