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 e9a5719
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 2024/day19.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package `2024`.day19

import scala.collection.concurrent.TrieMap
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}")
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 e9a5719

Please sign in to comment.