-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day08.scala
38 lines (30 loc) · 1.02 KB
/
Day08.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
35
36
37
38
package adventofcode.solutions
import adventofcode.Definitions.*
@main def Day08 = Day(8) { (input, part) =>
case class Vec(i: Int, j: Int):
infix def +(that: Vec): Vec = Vec(i + that.i, j + that.j)
infix def -(that: Vec): Vec = Vec(i - that.i, j - that.j)
infix def *(v: Int): Vec = Vec(i * v, j * v)
val grid = input.toLines.map(_.map {
case '.' => None
case c => Some(c)
})
val frequencies = grid.zipWithIndex
.flatMap((row, i) => row.zipWithIndex.collect { case (Some(c), j) => c -> Vec(i, j) })
.groupBy((f, _) => f).view.mapValues(_.map((_, p) => p)).toMap
def antennasCount(range: Range) =
val set =
for
f <- frequencies.keySet
pa <- frequencies(f)
pb <- frequencies(f)
if pa != pb
v = pa - pb
k <- range
p = pa + v * k
if grid.indices.contains(p.i) && grid(p.i).indices.contains(p.j)
yield p
set.size
part(1) = antennasCount(1 to 1)
part(2) = antennasCount(0 to Math.max(grid.size, grid.head.size))
}