-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day17.cs
219 lines (185 loc) · 8.28 KB
/
Day17.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
using System;
using System.Collections.Generic;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day17 : ISolver
{
// The solutions to parts 1 and 2 are very similar, but there are enough small differences that I couldn't find
// any nice way to abstract them away so I have just written each part separately.
//
// The solution is essentially A* using Manhattan Distance as the heuristic. I use a bucket queue and a bitset to
// iterate through the possible states in order. A state is essentially (int x, int y, bool isVertical) where x,y
// represents the position and isVertical represents if the last move to reach this space was a vertical or
// horizontal move. I pack these three values into a ushort, with the least significant bit representing isVertical
// and the bits above it equal to y * width + x. When I process a new state, I first check if it is seen in the
// bitset and if it isn't, I iterate through all the possible full strides I can make from my current position.
// This means that I am not moving one step at a time, but each turn I am alternating between a horizontal move and
// a vertical move.
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int width = input.IndexOf((byte)'\n');
int rowLength = width + 1;
int height = input.Length / rowLength;
// Create reusable buckets for both parts
List<ushort>[] buckets = new List<ushort>[1300];
for (int i = 0; i < 128; i++)
buckets[i] = new List<ushort>(1500);
for (int i = 128; i < buckets.Length; i++)
buckets[i] = buckets[i % 128];
int part1 = SolvePart1(input, width, height, buckets);
solution.SubmitPart1(part1);
for (int i = 0; i < 128; i++)
buckets[i].Clear();
int part2 = SolvePart2(input, width, height, buckets);
solution.SubmitPart2(part2);
}
public static int SolvePart1(ReadOnlySpan<byte> input, int width, int height, List<ushort>[] buckets)
{
int rowLength = width + 1;
int numStates = rowLength * height * 2;
int targetState = (height - 1) * rowLength + (width - 1);
const int xMul = 2;
int yMul = 2 * rowLength;
ulong[] seen = new ulong[(numStates - 1) / 64 + 1];
int bucketPtr = 0;
buckets[0].Add(0);
buckets[0].Add(1);
while (true)
{
List<ushort> bucket = buckets[bucketPtr];
for (int i = 0; i < bucket.Count; i++)
{
ushort element = bucket[i];
ref ulong seenBitset = ref seen[element / 64];
ulong elementBit = 1UL << element;
if ((seenBitset & elementBit) != 0)
continue;
seenBitset |= elementBit;
int rowOffset = Math.DivRem(element, 2, out int isHorizontal);
if (rowOffset == targetState)
return bucketPtr + (width + height - 2);
int y = Math.DivRem(rowOffset, rowLength, out int x);
if (isHorizontal == 0)
{
int total = 0;
int maxX = Math.Min(4, width - x);
for (int x2 = 1; x2 < maxX; x2++)
{
total += input[rowOffset + x2] - '0' - 1;
buckets[bucketPtr + total].Add((ushort)(element + xMul * x2 + 1));
}
total = 0;
int minX = Math.Max(-3, -x);
for (int x2 = -1; x2 >= minX; x2--)
{
total += input[rowOffset + x2] - '0' + 1;
buckets[bucketPtr + total].Add((ushort)(element + xMul * x2 + 1));
}
}
else
{
int total = 0;
int maxY = Math.Min(4, height - y);
for (int y2 = 1; y2 < maxY; y2++)
{
total += input[rowOffset + rowLength * y2] - '0' - 1;
buckets[bucketPtr + total].Add((ushort)(element + yMul * y2 - 1));
}
total = 0;
int minY = Math.Max(-3, -y);
for (int y2 = -1; y2 >= minY; y2--)
{
total += input[rowOffset + rowLength * y2] - '0' + 1;
buckets[bucketPtr + total].Add((ushort)(element + yMul * y2 - 1));
}
}
}
bucket.Clear();
bucketPtr++;
}
}
public static int SolvePart2(ReadOnlySpan<byte> input, int width, int height, List<ushort>[] buckets)
{
int rowLength = width + 1;
int numStates = rowLength * height * 2;
int targetState = (height - 1) * rowLength + (width - 1);
const int xMul = 2;
int yMul = 2 * rowLength;
ulong[] seen = new ulong[(numStates - 1) / 64 + 1];
int bucketPtr = 0;
buckets[0].Add(0);
buckets[0].Add(1);
while (true)
{
List<ushort> bucket = buckets[bucketPtr];
for (int i = 0; i < bucket.Count; i++)
{
ushort element = bucket[i];
ulong elementBit = 1UL << element;
if ((seen[element / 64] & elementBit) != 0)
continue;
seen[element / 64] |= elementBit;
int rowOffset = Math.DivRem(element, 2, out int isHorizontal);
if (rowOffset == targetState)
return bucketPtr + (width + height - 2);
int y = Math.DivRem(rowOffset, rowLength, out int x);
if (isHorizontal == 0)
{
if (x < width - 4)
{
int total = 0;
for (int x2 = 1; x2 < 4; x2++)
total += input[rowOffset + x2] - '0' - 1;
int maxX = Math.Min(11, width - x);
for (int x2 = 4; x2 < maxX; x2++)
{
total += input[rowOffset + x2] - '0' - 1;
buckets[bucketPtr + total].Add((ushort)(element + xMul * x2 + 1));
}
}
if (x >= 4)
{
int total = 0;
for (int x2 = -1; x2 >= -3; x2--)
total += input[rowOffset + x2] - '0' + 1;
int minX = Math.Max(-10, -x);
for (int x2 = -4; x2 >= minX; x2--)
{
total += input[rowOffset + x2] - '0' + 1;
buckets[bucketPtr + total].Add((ushort)(element + xMul * x2 + 1));
}
}
}
else
{
if (y < height - 4)
{
int total = 0;
for (int y2 = 1; y2 < 4; y2++)
total += input[rowOffset + rowLength * y2] - '0' - 1;
int maxY = Math.Min(11, height - y);
for (int y2 = 4; y2 < maxY; y2++)
{
total += input[rowOffset + rowLength * y2] - '0' - 1;
buckets[bucketPtr + total].Add((ushort)(element + yMul * y2 - 1));
}
}
if (y >= 4)
{
int total = 0;
for (int y2 = -1; y2 >= -3; y2--)
total += input[rowOffset + rowLength * y2] - '0' + 1;
int minY = Math.Max(-10, -y);
for (int y2 = -4; y2 >= minY; y2--)
{
total += input[rowOffset + rowLength * y2] - '0' + 1;
buckets[bucketPtr + total].Add((ushort)(element + yMul * y2 - 1));
}
}
}
}
bucket.Clear();
bucketPtr++;
}
}
}