-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day10.scala
39 lines (27 loc) · 1020 Bytes
/
Day10.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
39
package adventofcode.solutions
import adventofcode.Definitions.*
@main def Day10 = Day(10) { (input, part) =>
case class Vec(i: Int, j: Int):
infix def +(that: Vec): Vec = Vec(i + that.i, j + that.j)
val grid = input.toLines.map(_.map(_.asDigit))
val zeros = grid.indices.flatMap(i => grid(i).indices.filter(j => grid(i)(j) == 0).map(j => Vec(i, j)))
val adjacent =
for
i <- -1 to 1
j <- -1 to 1
if i.abs + j.abs == 1
yield Vec(i, j)
def inBounds(vec: Vec): Boolean = grid.indices.contains(vec.i) && grid(vec.i).indices.contains(vec.j)
def explore(nodes: Iterable[Vec], i: Int): Int =
if i < 10 then
val nextNodes = nodes
.flatMap(n => adjacent.map(n + _))
.filter(inBounds)
.filter(v => grid(v.i)(v.j) == i)
explore(nextNodes, i + 1)
else
nodes.size
def count(factory: Vec => Iterable[Vec]): Int = zeros.map(zero => explore(factory(zero), 1)).sum
part(1) = count(Set.apply(_))
part(2) = count(Seq.apply(_))
}