Advertisement
mgla

Advent of Code - 2024 - Day 19

Dec 19th, 2024 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.64 KB | None | 0 0
  1. var input = await File.ReadAllLinesAsync("input.txt");
  2. var towels = input[0].Split(", ").ToArray();
  3. var designs = input[2..];
  4. var cache = new Dictionary<string, long>();
  5.  
  6. var results = designs.Select(Check).ToArray();
  7. Console.WriteLine($"Part 1: {results.Count(r => r > 0)}");
  8. Console.WriteLine($"Part 2: {results.Sum()}");
  9.  
  10. return;
  11.  
  12. long Check(string design)
  13. {
  14.     if (cache.TryGetValue(design, out var cached))
  15.     {
  16.         return cached;
  17.     }
  18.  
  19.     var result = towels.Contains(design) ? 1L : 0L;
  20.     result += towels.Where(design.StartsWith).Sum(t => Check(design[t.Length..]));
  21.     cache[design] = result;
  22.  
  23.     return result;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement