Advent of Code 2024 - Day 19Linen Layout
| Problem statement | Source code | Tags: Memoization
This problem pays tribute to 2023 day 12.
Essentially we are given a list of strings and we want to use them to construct a target string. This problem is recursive in nature, because each time, we find a suitable string that is a prefix of the target string, remove it and match the rest. For example, given the target string abc and the towels ["a", "ab", "bc"], we can either use a and then try to construct bc, or we can use ab and then try to construct c.
The problem is that we may be recomputing ways with the same target string multiple times. The easy way out is to memoize each input for ways.
Now part 1 is the count of ways_to_construct > 0, and part 2 is the sum of ways_to_construct.