-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDay14.cs
More file actions
232 lines (197 loc) · 8.8 KB
/
Day14.cs
File metadata and controls
232 lines (197 loc) · 8.8 KB
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
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)
{
var width = input.IndexOf((byte)'\n');
var rowLength = width + 1;
var height = input.Length / rowLength;
var walls = new uint[(height + 2) * 4];
var rocks = new uint[(height + 2) * 4];
Array.Fill(walls, 0xFFFFFFFFU);
ref var inputRef = ref MemoryMarshal.GetReference(input);
for (var i = 0; i < height; i++)
{
var index = 4 * (i + 1);
for (var 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();
}
var lastWallsRow = ~((1U << width) - 1); // add fake walls on the edges
uint lastRocksRow = 0;
for (var j = (index % 4) * Vector256<byte>.Count; j < width; j++)
{
var 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 (var j = 4 * (i + 1); j <= index; j++)
{
var 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();
var part1 = ScoreGrid();
solution.SubmitPart1(part1);
TiltSouthEast();
var d = new Dictionary<int, int>(300);
var scores = new List<int>(300);
var iterations = 0;
while (true)
{
var hash = HashGrid();
if (d.TryGetValue(hash, out var j))
{
var cycleLen = iterations - j;
var cycleOffset = (1000000000 - iterations) % cycleLen;
solution.SubmitPart2(scores[j + cycleOffset - 1]);
break;
}
else
{
d[hash] = iterations;
scores.Add(ScoreGrid());
}
TiltNorthWest();
TiltSouthEast();
iterations++;
}
int ScoreGrid()
{
var score = 0;
for (var 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 var rocksRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(rocks), 4);
ref var wallsRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(walls), 4);
var prevFreeSpace = Vector128<uint>.Zero;
for (var row = 0; row < height; row++)
{
var rocksVec = Vector128.LoadUnsafe(ref rocksRef);
ref var nextRocksRef = ref Unsafe.Add(ref rocksRef, 4);
ref var nextWallsRef = ref Unsafe.Add(ref wallsRef, 4);
var freeSpace = ~(rocksVec | Vector128.LoadUnsafe(ref wallsRef) | Vector128.LoadUnsafe(ref nextWallsRef) | prevFreeSpace);
while (freeSpace != Vector128<uint>.Zero)
{
var 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;
}
var 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)
{
var shifted = ShiftLeftByOne(walls);
var newWalls = walls | (shifted & rocks);
if (walls == newWalls)
break;
walls = newWalls;
}
var movingRocks = ~walls & rocks;
if (movingRocks == Vector128<uint>.Zero)
break;
rocks = (rocks & walls) | ShiftRightByOne(movingRocks);
}
return rocks;
}
}
void TiltSouthEast()
{
ref var rocksRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(rocks), height * 4);
ref var wallsRef = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(walls), height * 4);
var prevFreeSpace = Vector128<uint>.Zero;
for (var row = 0; row < height; row++)
{
var rocksVec = Vector128.LoadUnsafe(ref rocksRef);
ref var nextRocksRef = ref Unsafe.Subtract(ref rocksRef, 4);
ref var nextWallsRef = ref Unsafe.Subtract(ref wallsRef, 4);
var freeSpace = ~(rocksVec | Vector128.LoadUnsafe(ref wallsRef) | Vector128.LoadUnsafe(ref nextWallsRef) | prevFreeSpace);
while (freeSpace != Vector128<uint>.Zero)
{
var newRocksVec = Vector128.LoadUnsafe(ref nextRocksRef);
var 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;
}
var 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)
{
var shifted = ShiftRightByOne(walls);
var newWalls = walls | (shifted & rocks);
if (walls == newWalls)
break;
walls = newWalls;
}
var movingRocks = ~walls & rocks;
if (movingRocks == Vector128<uint>.Zero)
break;
rocks = (rocks & walls) | ShiftLeftByOne(movingRocks);
}
return rocks;
}
}
static Vector128<uint> ShiftLeftByOne(Vector128<uint> v)
{
return (v << 1) | Vector128.Shuffle(v >> 31, Vector128.Create(3U, 0U, 1U, 2U));
}
static Vector128<uint> ShiftRightByOne(Vector128<uint> v)
{
return (v >> 1) | Vector128.Shuffle(v << 31, Vector128.Create(1U, 2U, 3U, 0U));
}
}
}