-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay14.cs
226 lines (191 loc) · 8.94 KB
/
Day14.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
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day14 : ISolver
{
// Assumes grid width <= 131
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int width = input.IndexOf((byte)'\n');
int rowLength = width + 1;
int height = input.Length / rowLength;
uint[] walls = new uint[(height + 2) * 4];
uint[] rocks = new uint[(height + 2) * 4];
Array.Fill(walls, 0xFFFFFFFFU);
ref byte inputRef = ref MemoryMarshal.GetReference(input);
for (int i = 0; i < height; i++)
{
int index = 4 * (i + 1);
for (int j = 0; j + Vector256<byte>.Count < width; j += Vector256<byte>.Count, index++)
{
var v = Vector256.LoadUnsafe(ref Unsafe.Add(ref inputRef, j));
walls[index] = Vector256.Equals(v, Vector256.Create((byte)'#')).ExtractMostSignificantBits();
rocks[index] = Vector256.Equals(v, Vector256.Create((byte)'O')).ExtractMostSignificantBits();
}
uint lastWallsRow = ~((1U << width) - 1); // add fake walls on the edges
uint lastRocksRow = 0;
for (int j = (index % 4) * Vector256<byte>.Count; j < width; j++)
{
byte c = Unsafe.Add(ref inputRef, j);
lastWallsRow |= (c == '#' ? 1U : 0U) << j;
lastRocksRow |= (c == 'O' ? 1U : 0U) << j;
}
walls[index] = lastWallsRow;
rocks[index] = lastRocksRow;
// add wall on the left side
uint wallsCarry = 1;
uint rocksCarry = 0;
for (int j = 4 * (i + 1); j <= index; j++)
{
uint prev = walls[j];
walls[j] = (prev << 1) | wallsCarry;
wallsCarry = prev >> 31;
prev = rocks[j];
rocks[j] = (prev << 1) | rocksCarry;
rocksCarry = prev >> 31;
}
inputRef = ref Unsafe.Add(ref inputRef, rowLength);
}
TiltNorthWest();
int part1 = ScoreGrid();
solution.SubmitPart1(part1);
TiltSouthEast();
var d = new Dictionary<int, int>(300);
var scores = new List<int>(300);
int iterations = 0;
while (true)
{
int hash = HashGrid();
if (d.TryGetValue(hash, out int j))
{
int cycleLen = iterations - j;
int cycleOffset = (1000000000 - iterations) % cycleLen;
solution.SubmitPart2(scores[j + cycleOffset - 1]);
break;
}
else
{
d[hash] = iterations;
scores.Add(ScoreGrid());
}
TiltNorthWest();
TiltSouthEast();
iterations++;
}
int ScoreGrid()
{
int score = 0;
for (int i = 4; i < rocks.Length - 4; i++)
score += BitOperations.PopCount(rocks[i]) * (height - (i / 4) + 1);
return score;
}
int HashGrid()
{
var hashCode = new HashCode();
hashCode.AddBytes(MemoryMarshal.Cast<uint, byte>(rocks.AsSpan().Slice(4, rocks.Length - 8)));
return hashCode.ToHashCode();
}
void TiltNorthWest()
{
ref uint rocksRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(rocks), 4);
ref uint wallsRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(walls), 4);
Vector128<uint> prevFreeSpace = Vector128<uint>.Zero;
for (int row = 0; row < height; row++)
{
Vector128<uint> rocksVec = Vector128.LoadUnsafe(ref rocksRef);
ref uint nextRocksRef = ref Unsafe.Add(ref rocksRef, 4);
ref uint nextWallsRef = ref Unsafe.Add(ref wallsRef, 4);
Vector128<uint> freeSpace = ~(rocksVec | Vector128.LoadUnsafe(ref wallsRef) | Vector128.LoadUnsafe(ref nextWallsRef) | prevFreeSpace);
while (freeSpace != Vector128<uint>.Zero)
{
Vector128<uint> rocksToAdd = Vector128.LoadUnsafe(ref nextRocksRef) & freeSpace;
rocksVec |= rocksToAdd;
Vector128.StoreUnsafe(Vector128.LoadUnsafe(ref nextRocksRef) ^ rocksToAdd, ref nextRocksRef);
nextRocksRef = ref Unsafe.Add(ref nextRocksRef, 4);
nextWallsRef = ref Unsafe.Add(ref nextWallsRef, 4);
freeSpace &= ~Vector128.LoadUnsafe(ref nextWallsRef) & ~rocksToAdd;
}
Vector128<uint> tiltedWest = TiltRowWest(Vector128.LoadUnsafe(ref wallsRef), rocksVec);
Vector128.StoreUnsafe(tiltedWest, ref rocksRef);
prevFreeSpace = ~(rocksVec | Vector128.LoadUnsafe(ref wallsRef));
rocksRef = ref Unsafe.Add(ref rocksRef, 4);
wallsRef = ref Unsafe.Add(ref wallsRef, 4);
}
static Vector128<uint> TiltRowWest(Vector128<uint> walls, Vector128<uint> rocks)
{
while (true)
{
// mark any rocks that are in their correct place as walls
while (true)
{
Vector128<uint> shifted = ShiftLeftByOne(walls);
Vector128<uint> newWalls = walls | (shifted & rocks);
if (walls == newWalls)
break;
walls = newWalls;
}
Vector128<uint> movingRocks = ~walls & rocks;
if (movingRocks == Vector128<uint>.Zero)
break;
rocks = (rocks & walls) | ShiftRightByOne(movingRocks);
}
return rocks;
}
}
void TiltSouthEast()
{
ref uint rocksRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(rocks), height * 4);
ref uint wallsRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(walls), height * 4);
Vector128<uint> prevFreeSpace = Vector128<uint>.Zero;
for (int row = 0; row < height; row++)
{
Vector128<uint> rocksVec = Vector128.LoadUnsafe(ref rocksRef);
ref uint nextRocksRef = ref Unsafe.Subtract(ref rocksRef, 4);
ref uint nextWallsRef = ref Unsafe.Subtract(ref wallsRef, 4);
Vector128<uint> freeSpace = ~(rocksVec | Vector128.LoadUnsafe(ref wallsRef) | Vector128.LoadUnsafe(ref nextWallsRef) | prevFreeSpace);
while (freeSpace != Vector128<uint>.Zero)
{
Vector128<uint> newRocksVec = Vector128.LoadUnsafe(ref nextRocksRef);
Vector128<uint> rocksToAdd = newRocksVec & freeSpace;
rocksVec |= rocksToAdd;
Vector128.StoreUnsafe(newRocksVec ^ rocksToAdd, ref nextRocksRef);
nextRocksRef = ref Unsafe.Subtract(ref nextRocksRef, 4);
nextWallsRef = ref Unsafe.Subtract(ref nextWallsRef, 4);
freeSpace &= ~Vector128.LoadUnsafe(ref nextWallsRef) & ~rocksToAdd;
}
Vector128<uint> tiltedEast = TiltRowEast(Vector128.LoadUnsafe(ref wallsRef), rocksVec);
Vector128.StoreUnsafe(tiltedEast, ref rocksRef);
prevFreeSpace = ~(rocksVec | Vector128.LoadUnsafe(ref wallsRef));
rocksRef = ref Unsafe.Subtract(ref rocksRef, 4);
wallsRef = ref Unsafe.Subtract(ref wallsRef, 4);
}
static Vector128<uint> TiltRowEast(Vector128<uint> walls, Vector128<uint> rocks)
{
while (true)
{
// mark any rocks that are in their correct place as walls
while (true)
{
Vector128<uint> shifted = ShiftRightByOne(walls);
Vector128<uint> newWalls = walls | (shifted & rocks);
if (walls == newWalls)
break;
walls = newWalls;
}
Vector128<uint> movingRocks = ~walls & rocks;
if (movingRocks == Vector128<uint>.Zero)
break;
rocks = (rocks & walls) | ShiftLeftByOne(movingRocks);
}
return rocks;
}
}
static Vector128<uint> ShiftLeftByOne(Vector128<uint> v) => (v << 1) | Vector128.Shuffle(v >> 31, Vector128.Create(3U, 0U, 1U, 2U));
static Vector128<uint> ShiftRightByOne(Vector128<uint> v) => (v >> 1) | Vector128.Shuffle(v << 31, Vector128.Create(1U, 2U, 3U, 0U));
}
}