Skip to content

Commit

Permalink
2024 Day19 refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
smabuk committed Dec 19, 2024
1 parent 3d97474 commit f178e5d
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions Solutions/2024/Day19.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static void LoadTowels(string[] input)

public static int Part1(string[] _)
=> _desiredDesigns
.Where(design => design.IsPossible(_towelPatterns))
.Count();
.Where(design => design.IsPossible(_towelPatterns))
.Count();

public static long Part2(string[] _)
=> _desiredDesigns
Expand All @@ -29,17 +29,10 @@ public static long Part2(string[] _)

public static bool IsPossible(this string design, List<string> patterns)
{
if (design is "") {
return true;
}

foreach (string pattern in patterns.Where(design.StartsWith)) {
if (design[pattern.Length..].IsPossible(patterns)) {
return true;
}
}

return false;
return design is "" ||
patterns
.Where(design.StartsWith)
.Any(pattern => design[pattern.Length..].IsPossible(patterns));
}

public static IEnumerable<long> AllPossible(this string design, List<string> patterns, Dictionary<(string, string), long> cache)
Expand All @@ -55,8 +48,7 @@ public static IEnumerable<long> AllPossible(this string design, List<string> pat
continue;
}

List<long> count = [.. design[pattern.Length..].AllPossible(patterns, cache)];
long sum = count.Sum();
long sum = design[pattern.Length..].AllPossible(patterns, cache).Sum();
cache.Add((design, pattern), sum);
yield return sum;
}
Expand Down

0 comments on commit f178e5d

Please sign in to comment.