diff --git a/src/main/scala/eu/sim642/adventofcode2024/Day25.scala b/src/main/scala/eu/sim642/adventofcode2024/Day25.scala index b083668a..c74ccb1a 100644 --- a/src/main/scala/eu/sim642/adventofcode2024/Day25.scala +++ b/src/main/scala/eu/sim642/adventofcode2024/Day25.scala @@ -8,16 +8,21 @@ 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 key.correspondsGrid(lock)((k, l) => !(k && l)) + if fits(key, lock) } yield ()).size } - def parseLockKeys(input: String): Seq[Grid[Boolean]] = input.split("\n\n").map(_.linesIterator.map(_.map(_ == '#').toVector).toVector).toSeq + 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