diff --git a/2024/day19.scala b/2024/day19.scala new file mode 100644 index 0000000..4c3b54f --- /dev/null +++ b/2024/day19.scala @@ -0,0 +1,24 @@ +package `2024`.day19 + +import scala.collection.concurrent.TrieMap +import scala.annotation.tailrec +import prelude.* + +case class Towel(val designs: Set[String]): + def combos(towel: String): Long = + val memo = TrieMap[String, Long]("" -> 1) + + def go(cur: String): Long = memo.getOrElseUpdate( + cur, + designs.view.filter(cur.startsWith).sumBy { p => go(cur.drop(p.size)) }, + ) + go(towel) + +@main def main = + val input = fromFile(".cache/2024/19.txt").getLines + val (towel, cases) = input.splitAt(2) |> ((a, b) => + (a.mkString.split(",").map(_.trim).toSet |> Towel.apply, b.toVector) + ) + val result = cases.map(towel.combos) + println(s"part1: ${result.count(_ > 0)}") + println(s"part2: ${result.sum}") diff --git a/2024/day19.test.scala b/2024/day19.test.scala new file mode 100644 index 0000000..41fd863 --- /dev/null +++ b/2024/day19.test.scala @@ -0,0 +1,19 @@ +package `2024`.day19 + +import munit.FunSuite + +class Test extends FunSuite: + test("example"): + val towel = Towel(Set("r", "wr", "b", "g", "bwu", "rb", "gb", "br")) + + val cases = Map[String, Long]( + "brwrr" -> 2, + "bggr" -> 1, + "gbbr" -> 4, + "rrbgbr" -> 6, + "ubwu" -> 0, + "bwurrg" -> 1, + "brgr" -> 2, + "bbrgwb" -> 0, + ) + cases.foreach((a, b) => assertEquals(towel.combos(a), b))