diff --git a/Solutions/2024/Day19.cs b/Solutions/2024/Day19.cs index 0922c47..0b00a89 100644 --- a/Solutions/2024/Day19.cs +++ b/Solutions/2024/Day19.cs @@ -22,6 +22,11 @@ public static int Part1(string[] _) .Where(design => design.IsPossible(_towelPatterns)) .Count(); + public static long Part2(string[] _) + => _desiredDesigns + .Select(design => design.AllPossible(_towelPatterns, []).Sum()) + .Sum(); + public static bool IsPossible(this string design, List patterns) { if (design is "") { @@ -37,5 +42,23 @@ public static bool IsPossible(this string design, List patterns) return false; } - public static string Part2(string[] input, params object[]? args) => NO_SOLUTION_WRITTEN_MESSAGE; + public static IEnumerable AllPossible(this string design, List patterns, Dictionary<(string, string), long> cache) + { + if (design is "") { + yield return 1; + yield break; + } + + foreach (string pattern in patterns.Where(design.StartsWith)) { + if (cache.TryGetValue((design, pattern), out long cacheCount)) { + yield return cacheCount; + continue; + } + + List count = [.. design[pattern.Length..].AllPossible(patterns, cache)]; + long sum = count.Sum(); + cache.Add((design, pattern), sum); + yield return sum; + } + } } diff --git a/Tests/2024/Tests_19.cs b/Tests/2024/Tests_19.cs index 0ea10be..00986f1 100644 --- a/Tests/2024/Tests_19.cs +++ b/Tests/2024/Tests_19.cs @@ -22,4 +22,59 @@ public void Part1(string input, int expected) _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART1, input), out int actual); actual.ShouldBe(expected); } + + [Theory] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + brwrr + bggr + gbbr + rrbgbr + ubwu + bwurrg + brgr + bbrgwb + """, 16)] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + ubwu + bbrgwb + """, 0)] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + brwrr + """, 2)] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + bggr + """, 1)] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + gbbr + """, 4)] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + rrbgbr + """, 6)] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + bwurrg + """, 1)] + [InlineData(""" + r, wr, b, g, bwu, rb, gb, br + + brgr + """, 2)] + public void Part2(string input, int expected) + { + _ = int.TryParse(SolutionRouter.SolveProblem(YEAR, DAY, PART2, input), out int actual); + actual.ShouldBe(expected); + } }