-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
140 lines (112 loc) · 3.64 KB
/
Program.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using System.Collections.Concurrent;
using AdventOfCode.Common;
var stones = Resources.GetInputFileLines()
.First()
.SplitToNumbers(" ")
.Select(v => new Stone((ulong)v, 0))
.ToArray();
var differentStones = new HashSet<ulong>();
foreach (var stone in stones)
{
foreach (var s in GetDifferentStones(stone).Keys.Concat(stones.Select(s => s.Number)))
{
differentStones.Add(s);
}
}
Console.WriteLine($"Found {differentStones.Count} different stones");
const int step = 25;
var stoneExpansionMap = new ConcurrentDictionary<ulong, ConcurrentDictionary<ulong, ulong>>();
Parallel.ForEach(differentStones, stone =>
{
stoneExpansionMap[stone] = ExpandStones([new Stone(stone, 0)], step, []);
});
Console.WriteLine("Stone expansion map finished");
var stonesAfterStep = ExpandStones(stones, step, []);
Console.WriteLine($"Stones after {step}: {stonesAfterStep.Values.Sum()}");
for (int i = step; i < step * 4;)
{
var stonesAfter = new ConcurrentDictionary<ulong, ulong>();
foreach (var stone in stonesAfterStep)
{
foreach (var newStone in stoneExpansionMap[stone.Key])
{
var newStoneCount = newStone.Value * stone.Value;
stonesAfter.AddOrUpdate(newStone.Key, newStoneCount, (_, previous) => previous + newStoneCount);
}
}
i += step;
Console.WriteLine($"Stones after {i}: {stonesAfter.Values.Sum()}");
stonesAfterStep = stonesAfter;
}
static ConcurrentDictionary<ulong, ulong> ExpandStones(IEnumerable<Stone> stones, int blinkTimes, ConcurrentDictionary<ulong, ulong> expandedStones)
{
var queue = new ConcurrentQueue<Stone>();
foreach (var s in stones)
{
queue.Enqueue(s);
while (!queue.IsEmpty)
{
Parallel.ForEach(queue, _ =>
{
if (!queue.TryDequeue(out Stone stone)) return;
if (stone.Generation == blinkTimes)
{
expandedStones.AddOrUpdate(stone.Number, 1, (_, value) => value + 1);
return;
}
var newGeneration = stone.Generation + 1;
var newStones = Blink(stone.Number);
queue.Enqueue(new Stone(newStones.Item1, newGeneration));
if (newStones.Item2.HasValue)
{
queue.Enqueue(new Stone(newStones.Item2.Value, newGeneration));
}
});
}
}
return expandedStones;
}
static Dictionary<ulong, int> GetDifferentStones(Stone startingStone)
{
Dictionary<ulong, int> differentStones = new()
{
[startingStone.Number] = startingStone.Generation
};
var queue = new Queue<Stone>();
queue.Enqueue(startingStone);
while (queue.Count > 0)
{
if (!queue.TryDequeue(out Stone n)) continue;
var (first, second) = Blink(n.Number);
var ng = n.Generation + 1;
if (differentStones.TryAdd(first, ng))
{
queue.Enqueue(new Stone(first, ng));
}
if (second.HasValue)
{
if (differentStones.TryAdd(second.Value, ng))
{
queue.Enqueue(new Stone(second.Value, ng));
}
}
}
return differentStones;
}
static (ulong, ulong?) Blink(ulong number)
{
if (number == 0)
{
return (1, null);
}
var digits = (int)(Math.Log10(number) + 1);
if (digits % 2 != 0)
{
return (number * 2024, null);
}
var div = (ulong)Math.Pow(10, digits / 2);
var left = number / div;
var right = number - left * div;
return (left, right);
}
readonly file record struct Stone(ulong Number, int Generation);