From 326a6c8dbab44ce2bcc52b80eeb1b9cb1d9d5437 Mon Sep 17 00:00:00 2001 From: Simmo Saan Date: Wed, 25 Dec 2024 09:33:19 +0200 Subject: [PATCH] Clean up 2024 day 25 --- src/main/scala/eu/sim642/adventofcode2024/Day25.scala | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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