-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay19.cs
53 lines (42 loc) · 1.5 KB
/
Day19.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
namespace Aoc2024Net.Days
{
internal sealed class Day19 : Day
{
public override object? SolvePart1() =>
GetArrangementCounts().Count;
public override object? SolvePart2() =>
GetArrangementCounts().Sum(c => c.Value);
private Dictionary<string, long> GetArrangementCounts()
{
var linesGroups = InputData.GetInputLinesGroups();
var towels = linesGroups[0][0].Split(',', StringSplitOptions.TrimEntries).ToArray();
var designs = linesGroups[1];
var counts = new Dictionary<string, long>();
foreach (var design in designs)
{
GetArrangementCounts(design, towels, counts);
}
return counts
.Where(c => designs.Contains(c.Key) && c.Value > 0)
.ToDictionary(
c => c.Key,
c => c.Value);
}
private static long GetArrangementCounts(
string design,
string[] towels,
Dictionary<string, long> counts)
{
if (counts.TryGetValue(design, out var c))
return c;
var result = towels.LongCount(t => t == design);
foreach (var t in towels)
{
if (!design.StartsWith(t))
continue;
result += GetArrangementCounts(design[t.Length..], towels, counts);
}
return counts[design] = result;
}
}
}