Skip to content

Commit

Permalink
2024 Day19 Solved
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 19, 2024
1 parent 4bf5244 commit 3d97474
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Solutions/2024/Day19.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> patterns)
{
if (design is "") {
Expand All @@ -37,5 +42,23 @@ public static bool IsPossible(this string design, List<string> patterns)
return false;
}

public static string Part2(string[] input, params object[]? args) => NO_SOLUTION_WRITTEN_MESSAGE;
public static IEnumerable<long> AllPossible(this string design, List<string> 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<long> count = [.. design[pattern.Length..].AllPossible(patterns, cache)];
long sum = count.Sum();
cache.Add((design, pattern), sum);
yield return sum;
}
}
}
55 changes: 55 additions & 0 deletions Tests/2024/Tests_19.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 3d97474

Please sign in to comment.