-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay18.cs
253 lines (213 loc) · 8.16 KB
/
Day18.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using AdventOfCode.CSharp.Common;
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace AdventOfCode.CSharp.Y2021.Solvers;
public class Day18 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
const int MaxSnailfishCount = 128;
Span<byte> snailFishes = stackalloc byte[16 * MaxSnailfishCount];
snailFishes.Fill(255); // 255 indicates that number is empty
int snailFishCount = ParseAllSnailfish(input, snailFishes);
Span<byte> snailfishSum = stackalloc byte[16];
snailFishes.Slice(0, 16).CopyTo(snailfishSum);
int part2 = 0;
for (int i = 1; i < snailFishCount; i++)
{
// Part 1
Span<byte> snailFish = snailFishes.Slice(i * 16, 16);
AddSnailfish(snailfishSum, snailFish);
// Part 2
for (int j = 0; j < i; j++)
{
Span<byte> otherSnailfish = snailFishes.Slice(j * 16, 16);
int magnitude1 = AddAndGetMagnitude(snailFish, otherSnailfish);
if (magnitude1 > part2)
part2 = magnitude1;
int magnitude2 = AddAndGetMagnitude(otherSnailfish, snailFish);
if (magnitude2 > part2)
part2 = magnitude2;
}
}
int part1 = GetMagnitude(snailfishSum);
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static int AddAndGetMagnitude(Span<byte> fish1, Span<byte> fish2)
{
Span<byte> part2SnailfishSum = stackalloc byte[16];
fish1.CopyTo(part2SnailfishSum);
AddSnailfish(part2SnailfishSum, fish2);
return GetMagnitude(part2SnailfishSum);
}
private static int ParseAllSnailfish(ReadOnlySpan<byte> input, Span<byte> snailFishes)
{
int snailFishCount = 0;
int inputIndex = 0;
while (inputIndex < input.Length)
{
ParseSnailfishLine(input, ref inputIndex, snailFishes.Slice(16 * snailFishCount++, 16));
inputIndex++; // skip newline
}
return snailFishCount;
}
private static void ParseSnailfishLine(ReadOnlySpan<byte> input, ref int inputIndex, Span<byte> snailfish)
{
byte c = input[inputIndex++];
if (c == '[')
{
int halfLen = snailfish.Length / 2;
ParseSnailfishLine(input, ref inputIndex, snailfish.Slice(0, halfLen));
inputIndex++; // skip comma
ParseSnailfishLine(input, ref inputIndex, snailfish.Slice(halfLen));
inputIndex++; // skip end bracket
}
else
{
snailfish[0] = (byte)(c - '0');
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddSnailfish(Span<byte> fish, ReadOnlySpan<byte> fishToAdd)
{
AddSnailfishWithExplosions(fish, fishToAdd, out int fishBitset, out int needsSplittingBitset);
while (needsSplittingBitset != 0)
{
// Get index of next number that needs splitting
// See https://lemire.me/blog/2018/02/21/iterating-over-set-bits-quickly/ for explanation
int t = needsSplittingBitset & -needsSplittingBitset;
int indexToSplit = BitOperations.TrailingZeroCount(t);
needsSplittingBitset ^= t;
SplitAtIndex(fish, indexToSplit, ref fishBitset, ref needsSplittingBitset);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void AddSnailfishWithExplosions(Span<byte> fish, ReadOnlySpan<byte> fishToAdd, out int fishBitset, out int needsSplittingBitset)
{
byte carryRight = 0;
int leftIndex = 255;
fishBitset = 0;
needsSplittingBitset = 0;
// Add first fish
for (int i = 0; i < 8; i++)
{
byte left = fish[i * 2];
if (left != 255)
{
left += carryRight;
carryRight = 0;
byte right = fish[i * 2 + 1];
if (right != 255)
{
if (leftIndex != 255)
{
int newAmount = fish[leftIndex] += left;
if (newAmount >= 10)
needsSplittingBitset |= 1 << leftIndex;
}
left = 0;
carryRight = right;
}
fishBitset |= 1 << i;
leftIndex = i;
if (left >= 10)
needsSplittingBitset |= 1 << i;
}
fish[i] = left;
}
// Add second fish
for (int i = 0; i < 8; i++)
{
int fishIndex = i + 8;
byte left = fishToAdd[i * 2];
if (left != 255)
{
left += carryRight;
carryRight = 0;
byte right = fishToAdd[i * 2 + 1];
if (right != 255)
{
if (leftIndex != 255)
{
int newAmount = fish[leftIndex] += left;
if (newAmount >= 10)
needsSplittingBitset |= 1 << leftIndex;
}
left = 0;
carryRight = right;
}
fishBitset |= 1 << fishIndex;
leftIndex = fishIndex;
if (left >= 10)
needsSplittingBitset |= 1 << fishIndex;
}
fish[fishIndex] = left;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void SplitAtIndex(Span<byte> fish, int i, ref int fishBitset, ref int needsSplittingBitset)
{
byte number = fish[i];
byte splitLeft = (byte)(number / 2);
byte splitRight = (byte)(number - splitLeft);
if ((i & 1) == 1 || (fishBitset & (1 << (i + 1))) != 0)
{
// Handle where split causes an explosion
fish[i] = 0;
// If the explosion causes a number to the left to go past 10 then we need to set i back to that point
for (int j = i - 1; j >= 0; j--)
{
if ((fishBitset & (1U << j)) != 0)
{
int newAmount = fish[j] += splitLeft;
if (newAmount >= 10)
needsSplittingBitset |= 1 << j;
break;
}
}
for (int j = i + 1; j < fish.Length; j++)
{
if ((fishBitset & (1U << j)) != 0)
{
int newAmount = fish[j] += splitRight;
if (newAmount >= 10)
needsSplittingBitset |= 1 << j;
break;
}
}
}
else
{
int distanceToNextFish = 2;
while ((i & distanceToNextFish) == 0 && (fishBitset & (1 << (i + distanceToNextFish))) == 0)
distanceToNextFish *= 2;
fish[i] = splitLeft;
int splitRightIndex = i + distanceToNextFish / 2;
fish[splitRightIndex] = splitRight;
fishBitset |= 1 << splitRightIndex;
// If the new left value is still greater than 10, we need to keep splitting it
if (splitLeft >= 10)
needsSplittingBitset |= 1 << i;
if (splitRight >= 10)
needsSplittingBitset |= 1 << splitRightIndex;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetMagnitude(Span<byte> snailFish)
{
static int GetMagnitudeInternal(Span<byte> snailFish, int firstNumber, int from, int to)
{
if (from + 1 == to)
return firstNumber;
int halfLen = from + (to - from) / 2;
int left = GetMagnitudeInternal(snailFish, firstNumber, from, halfLen);
int rightStart = snailFish[halfLen];
return rightStart == 255
? left
: 3 * left + 2 * GetMagnitudeInternal(snailFish, rightStart, halfLen, to);
}
return GetMagnitudeInternal(snailFish, snailFish[0], 0, 16);
}
}