Skip to content

Commit

Permalink
feat(2024/19): day 19
Browse files Browse the repository at this point in the history
  • Loading branch information
scarf005 committed Dec 19, 2024
1 parent 33b217f commit d336615
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions 2024/day19.scala
Original file line number Diff line number Diff line change
@@ -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(str: String): Long =
val memo = TrieMap[String, Long]("" -> 1)

def go(left: String): Long = memo.getOrElseUpdate(
left,
designs.view.filter(left.startsWith).sumBy { p => go(left.drop(p.size)) },
)
go(str)

@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}")
19 changes: 19 additions & 0 deletions 2024/day19.test.scala
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit d336615

Please sign in to comment.