File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
main/scala/eu/sim642/adventofcode2024
test/scala/eu/sim642/adventofcode2024 Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1
1
package eu .sim642 .adventofcode2024
2
2
3
+ import scala .collection .mutable
3
4
import scala .util .matching .Regex
4
5
5
6
object Day19 {
@@ -18,6 +19,28 @@ object Day19 {
18
19
input.designs.count(regex.matches)
19
20
}
20
21
22
+ def countWays (patterns : Seq [Towel ], design : Towel ): Long = {
23
+ val memo = mutable.Map .empty[Int , Long ]
24
+
25
+ def helper (i : Int ): Long = {
26
+ memo.getOrElseUpdate(i, {
27
+ if (i == design.size)
28
+ 1
29
+ else {
30
+ (for {
31
+ pattern <- patterns
32
+ if design.startsWith(pattern, i)
33
+ } yield helper(i + pattern.size)).sum
34
+ }
35
+ })
36
+ }
37
+
38
+ helper(0 )
39
+ }
40
+
41
+ def sumDesignWays (input : Input ): Long =
42
+ input.designs.map(countWays(input.patterns, _)).sum
43
+
21
44
def parsePatterns (s : String ): Seq [Towel ] = s.split(" , " ).toSeq
22
45
23
46
def parseDesigns (s : String ): Seq [Towel ] = s.linesIterator.toSeq
@@ -30,5 +53,8 @@ object Day19 {
30
53
31
54
def main (args : Array [String ]): Unit = {
32
55
println(countPossibleDesigns(parseInput(input)))
56
+ println(sumDesignWays(parseInput(input)))
57
+
58
+ // part 2: 1006410103498494 - too high (had i >= design.size - 1 in countWays)
33
59
}
34
60
}
Original file line number Diff line number Diff line change @@ -24,4 +24,12 @@ class Day19Test extends AnyFunSuite {
24
24
test(" Part 1 input answer" ) {
25
25
assert(countPossibleDesigns(parseInput(input)) == 226 )
26
26
}
27
+
28
+ test(" Part 2 examples" ) {
29
+ assert(sumDesignWays(parseInput(exampleInput)) == 16 )
30
+ }
31
+
32
+ test(" Part 2 input answer" ) {
33
+ assert(sumDesignWays(parseInput(input)) == 601201576113503L )
34
+ }
27
35
}
You can’t perform that action at this time.
0 commit comments