-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day19.cs
417 lines (346 loc) · 16.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace AdventOfCode.CSharp.Y2021.Solvers;
/// <summary>
/// Please come back later once I have made this better/more readable, but for now it's fast-ish
/// </summary>
public class Day19 : ISolver
{
public enum Axis { PosX, NegX, PosY, NegY, PosZ, NegZ }
readonly record struct Coord(int X, int Y, int Z) : IComparable<Coord>
{
public static readonly Coord Zero = new(0, 0, 0);
public int GetValueOnAxis(Axis axis) => axis switch
{
Axis.PosX => X,
Axis.NegX => -X,
Axis.PosY => Y,
Axis.NegY => -Y,
Axis.PosZ => Z,
Axis.NegZ => -Z,
_ => default
};
public Coord ApplyAxes(Axis XAxis, Axis YAxis, Axis ZAxis)
=> new(GetValueOnAxis(XAxis), GetValueOnAxis(YAxis), GetValueOnAxis(ZAxis));
public int GetManhattanDistance() => Math.Abs(X) + Math.Abs(Y) + Math.Abs(Z);
public int CompareTo(Coord other)
{
int c = X.CompareTo(other.X);
if (c != 0) return c;
c = Y.CompareTo(other.Y);
if (c != 0) return c;
return Z.CompareTo(other.Z);
}
public static Coord operator +(Coord left, Coord right)
=> new(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
public static Coord operator -(Coord left, Coord right)
=> new(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
}
record ScannerData(List<Coord> Coords);
record FixedScanner(Coord Center, ScannerData Data);
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
List<ScannerData> scanners = ParseInput(input);
int numScanners = scanners.Count;
var scannerCoordinates = new Coord[scanners.Count];
var foundScannersQueue = new Queue<FixedScanner>();
var beacons = new HashSet<Coord>();
int part2 = 0;
int foundScanners = 1;
ulong foundScannersBitset = 1;
scannerCoordinates[0] = Coord.Zero;
foundScannersQueue.Enqueue(new FixedScanner(Coord.Zero, scanners[0]));
foreach (Coord coord in scanners[0].Coords)
beacons.Add(coord);
while (foundScanners < numScanners && foundScannersQueue.TryDequeue(out FixedScanner? fixedScanner))
{
Span<Coord> fixedCoords = CollectionsMarshal.AsSpan(fixedScanner.Data.Coords);
fixedCoords.Sort();
for (int i = 0; i < numScanners; i++)
{
if ((foundScannersBitset & (1UL << i)) != 0)
continue;
ScannerData scanner = scanners[i];
Span<Coord> scannerCoords = CollectionsMarshal.AsSpan(scanner.Coords);
if (TryFindOverlap(fixedCoords, scannerCoords, out Coord relativePosition))
{
Coord center = fixedScanner.Center + relativePosition;
foreach (Coord coord in scannerCoords)
beacons.Add(coord + center);
for (int j = 0; j < foundScanners; j++)
{
int distance = (center - scannerCoordinates[j]).GetManhattanDistance();
if (distance > part2)
part2 = distance;
}
scannerCoordinates[foundScanners++] = center;
foundScannersBitset |= 1UL << i;
foundScannersQueue.Enqueue(new FixedScanner(center, scanner));
}
}
}
solution.SubmitPart1(beacons.Count);
solution.SubmitPart2(part2);
}
private static List<ScannerData> ParseInput(ReadOnlySpan<byte> input)
{
int inputIndex = 0;
var scanners = new List<ScannerData>();
while (inputIndex < input.Length)
scanners.Add(ReadScannerFromInput(input, ref inputIndex));
return scanners;
}
private static ScannerData ReadScannerFromInput(ReadOnlySpan<byte> input, ref int inputIndex)
{
// Skip first line containing scanner number
while (input[inputIndex++] != '\n')
continue;
var coords = new List<Coord>();
while (inputIndex < input.Length && input[inputIndex] != '\n')
{
int x = ReadIntegerFromInput(input, ',', ref inputIndex);
int y = ReadIntegerFromInput(input, ',', ref inputIndex);
int z = ReadIntegerFromInput(input, '\n', ref inputIndex);
coords.Add(new(x, y, z));
}
inputIndex++;
return new(coords);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadIntegerFromInput(ReadOnlySpan<byte> span, char until, ref int i)
{
// Assume that the first character is always a digit
byte c = span[i++];
int mul;
int ret;
if (c == '-')
{
mul = -1;
ret = 0;
}
else
{
mul = 1;
ret = c - '0';
}
byte cur;
while ((cur = span[i++]) != until)
ret = ret * 10 + (cur - '0');
return mul * ret;
}
private static bool TryFindOverlap(ReadOnlySpan<Coord> fixedCoords, Span<Coord> newCoords, out Coord relativePosition)
{
for (int axis = 0; axis < 6; axis++)
{
if (TryFindOverlapWithGivenXAxis(fixedCoords, newCoords, (Axis)axis, out relativePosition))
return true;
}
relativePosition = default;
return false;
}
private static bool TryFindOverlapWithGivenXAxis(ReadOnlySpan<Coord> fixedCoords, Span<Coord> newCoords, Axis axis, [NotNullWhen(returnValue: true)] out Coord relativePosition)
{
Span<byte> differenceCounts = stackalloc byte[4096];
int maxDifferenceCount = 1;
for (int newIndex = 0; newIndex < newCoords.Length - (12 - maxDifferenceCount); newIndex++)
{
int x2 = newCoords[newIndex].GetValueOnAxis(axis);
foreach (Coord fixedCoord in fixedCoords)
{
int diff = x2 - fixedCoord.X;
int diffIndex = diff + 2048;
byte differenceCount = differenceCounts[diffIndex]++;
if (differenceCount == 6 && TryFindOverlapWithGivenXAxisAndOffset(fixedCoords, newCoords, axis, diff, out relativePosition))
return true;
if (differenceCount > maxDifferenceCount)
maxDifferenceCount = differenceCount;
}
}
relativePosition = default;
return false;
}
private static bool TryFindOverlapWithGivenXAxisAndOffset(ReadOnlySpan<Coord> fixedCoords, Span<Coord> newCoords, Axis axis, int offset, out Coord relativePosition)
{
// This span will store the pairs of coordinates that have the same offset away on the x axis
Span<(Coord Fixed, Coord New)> matches = stackalloc (Coord Fixed, Coord New)[20];
int matchIndex = 0;
// Then, look through the remainder of the newCoords list to find fixed coords that are at the same offset
for (int newIndex = 0; newIndex < newCoords.Length; newIndex++)
{
int possibleMatchesLeft = newCoords.Length - newIndex;
if (matchIndex + possibleMatchesLeft < 12)
break;
Coord newCoord = newCoords[newIndex];
int newX = newCoord.GetValueOnAxis(axis);
int expectedX = newX - offset;
FindFixedCoordWithExpectedX(fixedCoords, expectedX, newCoord, matches, ref matchIndex);
}
if (matchIndex < 12)
{
relativePosition = default;
return false;
}
return TryFindOverlapFromMatchesOnGivenXAxis(matches, axis, newCoords, out relativePosition);
}
private static bool TryFindOverlapFromMatchesOnGivenXAxis(ReadOnlySpan<(Coord Fixed, Coord New)> matches, Axis axis, Span<Coord> newCoords, out Coord relativePosition)
{
switch (axis)
{
case Axis.PosX:
return
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosX, Axis.PosY, Axis.PosZ, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosX, Axis.NegY, Axis.NegZ, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosX, Axis.NegZ, Axis.PosY, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosX, Axis.PosZ, Axis.NegY, newCoords, out relativePosition);
case Axis.NegX:
return
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegX, Axis.PosZ, Axis.PosY, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegX, Axis.NegZ, Axis.NegY, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegX, Axis.NegY, Axis.PosZ, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegX, Axis.PosY, Axis.NegZ, newCoords, out relativePosition);
case Axis.PosY:
return
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosY, Axis.PosZ, Axis.PosX, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosY, Axis.NegZ, Axis.NegX, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosY, Axis.NegX, Axis.PosZ, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosY, Axis.PosX, Axis.NegZ, newCoords, out relativePosition);
case Axis.NegY:
return
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegY, Axis.PosX, Axis.PosZ, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegY, Axis.NegX, Axis.NegZ, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegY, Axis.NegZ, Axis.PosX, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegY, Axis.PosZ, Axis.NegX, newCoords, out relativePosition);
case Axis.PosZ:
return
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosZ, Axis.PosX, Axis.PosY, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosZ, Axis.NegX, Axis.NegY, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosZ, Axis.NegY, Axis.PosX, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.PosZ, Axis.PosY, Axis.NegX, newCoords, out relativePosition);
case Axis.NegZ:
return
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegZ, Axis.PosY, Axis.PosX, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegZ, Axis.NegY, Axis.NegX, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegZ, Axis.NegX, Axis.PosY, newCoords, out relativePosition) ||
TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(matches, Axis.NegZ, Axis.PosX, Axis.NegY, newCoords, out relativePosition);
default:
relativePosition = default;
return false;
}
}
private static int FindFixedCoordWithExpectedX(ReadOnlySpan<Coord> fixedCoords, int expectedX, Coord otherCoord, Span<(Coord Fixed, Coord New)> matches, ref int matchIndex)
{
int lo = 0;
int hi = fixedCoords.Length - 1;
while (lo <= hi)
{
int midIndex = (lo + hi) / 2;
Coord fixedCoord = fixedCoords[midIndex];
int x = fixedCoord.X;
if (x > expectedX)
{
hi = midIndex - 1;
}
else if (x < expectedX)
{
lo = midIndex + 1;
}
else
{
matches[matchIndex++] = (fixedCoord, otherCoord);
for (int i = midIndex - 1; i >= lo; i++)
{
fixedCoord = fixedCoords[i];
if (fixedCoord.X != expectedX)
break;
matches[matchIndex++] = (fixedCoord, otherCoord);
}
for (int i = midIndex + 1; i <= hi; i++)
{
fixedCoord = fixedCoords[i];
if (fixedCoord.X != expectedX)
break;
matches[matchIndex++] = (fixedCoord, otherCoord);
}
break;
}
}
return matchIndex;
}
private static bool TryFindOverlapFromMatchesOnGivenAxesAndFixCoords(ReadOnlySpan<(Coord Fixed, Coord New)> matches, Axis xAxis, Axis yAxis, Axis zAxis, Span<Coord> allCoords, out Coord center)
{
center = default;
// Use a faster solution if there are exactly 12 matches
if (matches.Length == 12)
{
(Coord firstMatchFixed, Coord firstMatchNew) = matches[0];
int expectedYDiff = firstMatchNew.GetValueOnAxis(yAxis) - firstMatchFixed.Y;
int expectedZDiff = firstMatchNew.GetValueOnAxis(zAxis) - firstMatchFixed.Z;
for (int i = 1; i < matches.Length; i++)
{
(Coord matchFixed, Coord matchNew) = matches[i];
int yDiff = matchNew.GetValueOnAxis(yAxis) - matchFixed.Y;
if (yDiff != expectedYDiff)
return false;
int zDiff = matchNew.GetValueOnAxis(zAxis) - matchFixed.Z;
if (zDiff != expectedZDiff)
return false;
}
center = firstMatchFixed - firstMatchNew.ApplyAxes(xAxis, yAxis, zAxis);
}
else
{
int matchesLeft = matches.Length;
uint matchesLeftBitset = (1U << (1 + matchesLeft)) - 1;
while (matchesLeft > 0)
{
int matchIndex = 0;
while ((matchesLeftBitset & (1U << matchIndex)) == 0)
matchIndex++;
int maxMatched = matchesLeft;
matchesLeft--;
(Coord firstMatchFixed, Coord firstMatchNew) = matches[matchIndex];
int expectedYDiff = firstMatchNew.GetValueOnAxis(yAxis) - firstMatchFixed.Y;
int expectedZDiff = firstMatchNew.GetValueOnAxis(zAxis) - firstMatchFixed.Z;
for (int i = matchIndex + 1; i < matches.Length; i++)
{
uint matchBit = 1U << matchIndex;
if ((matchesLeftBitset & matchBit) == 0)
continue;
(Coord matchFixed, Coord matchNew) = matches[i];
int yDiff = matchNew.GetValueOnAxis(yAxis) - matchFixed.Y;
if (yDiff != expectedYDiff)
{
if (--maxMatched < 12)
break;
continue;
}
int zDiff = matchNew.GetValueOnAxis(zAxis) - matchFixed.Z;
if (zDiff != expectedZDiff)
{
if (--maxMatched < 12)
break;
continue;
}
matchesLeft--;
matchesLeftBitset &= ~matchBit;
}
if (maxMatched >= 12)
{
center = firstMatchFixed - firstMatchNew.ApplyAxes(xAxis, yAxis, zAxis);
break;
}
else if (matchesLeft < 12)
{
return false;
}
}
}
for (int i = 0; i < allCoords.Length; i++)
allCoords[i] = allCoords[i].ApplyAxes(xAxis, yAxis, zAxis);
return true;
}
}