-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay11.cs
48 lines (41 loc) · 1.59 KB
/
Day11.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
namespace Aoc2024Net.Days
{
internal sealed class Day11 : Day
{
public override object? SolvePart1() =>
Solve(25);
public override object? SolvePart2() =>
Solve(75);
private long Solve(int blinkingCount)
{
var numbers = InputData
.GetInputText()
.Split(' ')
.Select(long.Parse)
.ToArray();
var stonesCountsCache = new Dictionary<(long, int), long>();
return numbers.Sum(n => CountStones(n, blinkingCount, stonesCountsCache));
}
private static long CountStones(long number, int blinkingCount, Dictionary<(long, int), long> stonesCountsCache)
{
if (blinkingCount == 0)
return 1;
if (stonesCountsCache.TryGetValue((number, blinkingCount), out var r))
return r;
var result = 0L;
if (number == 0)
result = CountStones(1, blinkingCount - 1, stonesCountsCache);
else
{
var s = number.ToString();
if (s.Length % 2 == 0)
result =
CountStones(long.Parse(s[..(s.Length / 2)]), blinkingCount - 1, stonesCountsCache) +
CountStones(long.Parse(s[(s.Length / 2)..]), blinkingCount - 1, stonesCountsCache);
else
result = CountStones(number * 2024, blinkingCount - 1, stonesCountsCache);
}
return stonesCountsCache[(number, blinkingCount)] = result;
}
}
}