-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDay25.scala
32 lines (23 loc) · 965 Bytes
/
Day25.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
package eu.sim642.adventofcode2024
import eu.sim642.adventofcodelib.Grid
import eu.sim642.adventofcodelib.GridImplicits._
object Day25 {
def isKey(grid: Grid[Boolean]): Boolean =
grid.head.forall(identity)
def fits(key: Grid[Boolean], lock: Grid[Boolean]): Boolean =
key.correspondsGrid(lock)((k, l) => !(k && l))
def countLockKeyFits(lockKeys: Seq[Grid[Boolean]]): Int = {
val (keys, locks) = lockKeys.partition(isKey)
(for {
key <- keys
lock <- locks
if fits(key, lock)
} yield ()).size
}
def parseLockKey(s: String): Grid[Boolean] = s.linesIterator.map(_.map(_ == '#').toVector).toVector
def parseLockKeys(input: String): Seq[Grid[Boolean]] = input.split("\n\n").map(parseLockKey).toSeq
lazy val input: String = scala.io.Source.fromInputStream(getClass.getResourceAsStream("day25.txt")).mkString.trim
def main(args: Array[String]): Unit = {
println(countLockKeyFits(parseLockKeys(input)))
}
}