-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day19.scala
29 lines (21 loc) · 954 Bytes
/
Day19.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package adventofcode.solutions
import adventofcode.Definitions.*
@main def Day19 = Day(19) { (input, part) =>
val (patterns, designs) = input.split(lineSeparator * 2).toSeq match
case Seq(a, b) => (a.split(", ").toSeq, b.split(lineSeparator).toSeq)
def countPossible(remaining: String, cache: Map[String, Long]): (Long, Map[String, Long]) =
if !cache.contains(remaining) then
if remaining.isEmpty then
(1, cache)
else
val (count, newCache) = patterns.filter(remaining.startsWith).foldLeft((0L, cache)) { case ((accCount, accCache), p) =>
val (newCount, newCache) = countPossible(remaining.substring(p.length, remaining.length), accCache)
(accCount + newCount, newCache)
}
(count, newCache + (remaining -> count))
else
(cache(remaining), cache)
val counts = designs.map(countPossible(_, Map.empty)._1)
part(1) = counts.count(_ > 0)
part(2) = counts.sum
}