-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day05.cs
261 lines (225 loc) · 9.17 KB
/
Day05.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
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace AdventOfCode.CSharp.Y2021.Solvers;
public class Day05 : ISolver
{
public readonly record struct Horizontal(int Y, int X1, int X2) : IComparable<Horizontal>
{
public int CompareTo(Horizontal other)
{
int c = Y.CompareTo(other.Y);
if (c != 0)
return c;
return X1.CompareTo(other.X1);
}
}
public readonly record struct Event(int Y, int X) : IComparable<Event>
{
public int CompareTo(Event other) => Y.CompareTo(other.Y);
}
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
const ulong onesFlag = 0b0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001UL;
var horizontals = new List<Horizontal>();
var verticalEvents = new List<Event>();
var posDiagEvents = new List<Event>();
var negDiagEvents = new List<Event>();
ParseInput(input, horizontals, verticalEvents, posDiagEvents, negDiagEvents, out int maxX, out int maxY);
horizontals.Sort();
verticalEvents.Sort();
posDiagEvents.Sort();
negDiagEvents.Sort();
// Add events to the end so that we don't have to do a bounds check
horizontals.Add(new Horizontal(int.MaxValue, 0, 0));
verticalEvents.Add(new Event(int.MaxValue, 0));
posDiagEvents.Add(new Event(int.MaxValue, 0));
negDiagEvents.Add(new Event(int.MaxValue, 0));
int horizIndex = 0;
int vertIndex = 0;
int posDiagIndex = 0;
int negDiagIndex = 0;
int countsLength = (maxX / 16) + 1;
Span<ulong> rowHorizontalCounts = stackalloc ulong[countsLength];
Span<ulong> currentVerticalCounts = stackalloc ulong[countsLength];
Span<ulong> currentPosDiagCounts = stackalloc ulong[countsLength];
Span<ulong> currentNegDiagCounts = stackalloc ulong[countsLength];
int part1 = 0;
int part2 = 0;
for (int y = 0; y <= (maxY * 2); y += 2)
{
ProcessHorizontals(horizontals, ref horizIndex, rowHorizontalCounts, y);
ProcessVerticals(verticalEvents, ref vertIndex, currentVerticalCounts, y);
ProcessPositiveDiagonals(posDiagEvents, ref posDiagIndex, currentPosDiagCounts, y);
ProcessNegativeDiagonals(negDiagEvents, ref negDiagIndex, currentNegDiagCounts, y);
for (int i = 0; i < countsLength; i++)
{
// Adding 6 to each value so that the high bit is set to 1 when there is an overlap
ulong part1Overlaps = rowHorizontalCounts[i] + currentVerticalCounts[i] + onesFlag * 6;
ulong part2Overlaps = part1Overlaps + currentPosDiagCounts[i] + currentNegDiagCounts[i];
part1 += BitOperations.PopCount(part1Overlaps & onesFlag * 8);
part2 += BitOperations.PopCount(part2Overlaps & onesFlag * 8);
}
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static void ParseInput(ReadOnlySpan<byte> input, List<Horizontal> horizontals, List<Event> verticalEvents, List<Event> posDiagEvents, List<Event> negDiagEvents, out int maxX, out int maxY)
{
int inputCursor = 0;
maxX = 0;
maxY = 0;
while (inputCursor < input.Length)
{
int x1 = ReadIntegerUntil(input, ',', ref inputCursor);
int y1 = ReadIntegerUntil(input, ' ', ref inputCursor);
inputCursor += "-> ".Length;
int x2 = ReadIntegerUntil(input, ',', ref inputCursor);
int y2 = ReadIntegerUntil(input, '\n', ref inputCursor);
if (x1 > maxX)
maxX = x1;
if (x2 > maxX)
maxX = x2;
if (y1 > maxY)
maxY = y1;
if (y2 > maxY)
maxY = y2;
int xDiff = x2 - x1;
int yDiff = y2 - y1;
if (y1 == y2)
{
horizontals.Add(new(y1 * 2, Math.Min(x1, x2), Math.Max(x1, x2)));
}
else if (x1 == x2)
{
verticalEvents.Add(new(Math.Min(y1, y2) * 2, x1));
verticalEvents.Add(new(Math.Max(y1, y2) * 2 + 1, x1));
}
else if (yDiff == xDiff)
{
if (y1 < y2)
{
posDiagEvents.Add(new(y1 * 2, x1));
posDiagEvents.Add(new(y2 * 2 + 1, x2));
}
else
{
posDiagEvents.Add(new(y2 * 2, x2));
posDiagEvents.Add(new(y1 * 2 + 1, x1));
}
}
else if (yDiff == -xDiff)
{
if (y1 < y2)
{
negDiagEvents.Add(new(y1 * 2, x1));
negDiagEvents.Add(new(y2 * 2 + 1, x2));
}
else
{
negDiagEvents.Add(new(y2 * 2, x2));
negDiagEvents.Add(new(y1 * 2 + 1, x1));
}
}
}
}
private static void ProcessHorizontals(List<Horizontal> horizontals, ref int horizIndex, Span<ulong> rowHorizontalCounts, int y)
{
const ulong onesFlag = 0b0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001_0001UL;
rowHorizontalCounts.Clear();
Horizontal horiz;
while ((horiz = horizontals[horizIndex]).Y == y)
{
(int x1Cell, int x1Offset) = Math.DivRem(horiz.X1, 16);
(int x2Cell, int x2Offset) = Math.DivRem(horiz.X2 + 1, 16);
if (x1Cell == x2Cell)
{
rowHorizontalCounts[x1Cell] += (((1UL << (x2Offset * 4)) - 1) ^ ((1UL << (x1Offset * 4)) - 1)) & onesFlag;
}
else
{
rowHorizontalCounts[x1Cell] += (ulong.MaxValue ^ ((1UL << (x1Offset * 4)) - 1)) & onesFlag;
for (int cell = x1Cell + 1; cell < x2Cell; cell++)
rowHorizontalCounts[cell] += onesFlag;
rowHorizontalCounts[x2Cell] += ((1UL << (x2Offset * 4)) - 1) & onesFlag;
}
horizIndex++;
}
}
private static void ProcessVerticals(List<Event> verticalEvents, ref int vertIndex, Span<ulong> currentVerticalCounts, int y)
{
Event vert;
while (vertIndex < verticalEvents.Count && (vert = verticalEvents[vertIndex]).Y == y - 1)
{
(int cell, int offset) = Math.DivRem(vert.X, 16);
currentVerticalCounts[cell] -= 1UL << (4 * offset);
vertIndex++;
}
while (vertIndex < verticalEvents.Count && (vert = verticalEvents[vertIndex]).Y == y)
{
(int cell, int offset) = Math.DivRem(vert.X, 16);
currentVerticalCounts[cell] += 1UL << (4 * offset);
vertIndex++;
}
}
private static void ProcessPositiveDiagonals(List<Event> posDiagEvents, ref int posDiagIndex, Span<ulong> currentPosDiagCounts, int y)
{
Event posDiag;
while ((posDiag = posDiagEvents[posDiagIndex]).Y == y - 1)
{
(int cell, int offset) = Math.DivRem(posDiag.X, 16);
currentPosDiagCounts[cell] -= 1UL << (4 * offset);
posDiagIndex++;
}
ulong carry = 0;
for (int i = 0; i < currentPosDiagCounts.Length; i++)
{
ulong c = currentPosDiagCounts[i];
ulong nextCarry = c >> 60;
currentPosDiagCounts[i] = (c << 4) + carry;
carry = nextCarry;
}
while ((posDiag = posDiagEvents[posDiagIndex]).Y == y)
{
(int cell, int offset) = Math.DivRem(posDiag.X, 16);
currentPosDiagCounts[cell] += 1UL << (4 * offset);
posDiagIndex++;
}
}
private static void ProcessNegativeDiagonals(List<Event> negDiagEvents, ref int negDiagIndex, Span<ulong> currentNegDiagCounts, int y)
{
Event negDiag;
while ((negDiag = negDiagEvents[negDiagIndex]).Y == y - 1)
{
(int cell, int offset) = Math.DivRem(negDiag.X, 16);
currentNegDiagCounts[cell] -= 1UL << (4 * offset);
negDiagIndex++;
}
ulong carry = 0;
for (int i = currentNegDiagCounts.Length - 1; i >= 0; i--)
{
ulong c = currentNegDiagCounts[i];
ulong nextCarry = c & 0b1111;
currentNegDiagCounts[i] = (c >> 4) + (carry << 60);
carry = nextCarry;
}
while ((negDiag = negDiagEvents[negDiagIndex]).Y == y)
{
(int cell, int offset) = Math.DivRem(negDiag.X, 16);
currentNegDiagCounts[cell] += 1UL << (4 * offset);
negDiagIndex++;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadIntegerUntil(ReadOnlySpan<byte> span, char c, ref int i)
{
// Assume that the first character is always a digit
int ret = span[i++] - '0';
byte cur;
while ((cur = span[i++]) != c)
ret = ret * 10 + (cur - '0');
return ret;
}
}