-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day09.cs
137 lines (111 loc) · 4.59 KB
/
Day09.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
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day09 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int part1 = 0;
int part2 = 0;
while (input.Length > 0)
{
int lineEndIndex = input.IndexOf((byte)'\n');
ReadOnlySpan<byte> line = input.Slice(0, lineEndIndex + 1);
ProcessLine(line, ref part1, ref part2);
input = input.Slice(lineEndIndex + 1);
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
[SkipLocalsInit]
private static void ProcessLine(ReadOnlySpan<byte> line, ref int part1, ref int part2)
{
// Assuming each line has 24 numbers or less
Span<int> lineNumbers = stackalloc int[24];
int n = LoadNumbersFromLine(line, lineNumbers);
ref int lineNumbersRef = ref MemoryMarshal.GetReference(lineNumbers);
Vector256<int> numberVec1 = Vector256.LoadUnsafe(ref lineNumbersRef);
Vector256<int> numberVec2 = Vector256.LoadUnsafe(ref lineNumbersRef, 8);
Vector256<int> numberVec3 = Vector256.LoadUnsafe(ref lineNumbersRef, 16);
int vecSize = n;
// handle case where data fits in 3 vectors
while (vecSize > 16)
{
int firstElement = numberVec1.GetElement(0);
part1 += numberVec3.GetElement(vecSize - 17);
part2 += (n - vecSize) % 2 == 1 ? -firstElement : firstElement;
Vector256<int> shifted1 = RotateLeftOneByte(numberVec1);
Vector256<int> shifted2 = RotateLeftOneByte(numberVec2);
Vector256<int> shifted3 = RotateLeftOneByte(numberVec3);
numberVec1 = MoveLastByteAcross(shifted1, shifted2) - numberVec1;
numberVec2 = MoveLastByteAcross(shifted2, shifted3) - numberVec2;
numberVec3 = shifted3 - numberVec3;
vecSize--;
if (numberVec1 == Vector256<int>.Zero && numberVec2 == Vector256<int>.Zero && FirstNBytesAreZero(numberVec3, vecSize - 16))
return;
}
// handle case where data fits in 2 vectors
while (vecSize > 8)
{
int firstElement = numberVec1.GetElement(0);
part1 += numberVec2.GetElement(vecSize - 9);
part2 += (n - vecSize) % 2 == 1 ? -firstElement : firstElement;
Vector256<int> shifted1 = RotateLeftOneByte(numberVec1);
Vector256<int> shifted2 = RotateLeftOneByte(numberVec2);
numberVec1 = MoveLastByteAcross(shifted1, shifted2) - numberVec1;
numberVec2 = shifted2 - numberVec2;
vecSize--;
if (numberVec1 == Vector256<int>.Zero && FirstNBytesAreZero(numberVec2, vecSize - 8))
return;
}
// handle case where data fits in 1 vector
while (vecSize > 0)
{
int firstElement = numberVec1.GetElement(0);
part1 += numberVec1.GetElement(vecSize - 1);
part2 += (n - vecSize) % 2 == 1 ? -firstElement : firstElement;
numberVec1 = RotateLeftOneByte(numberVec1) - numberVec1;
vecSize--;
if (FirstNBytesAreZero(numberVec2, vecSize))
return;
}
}
private static int LoadNumbersFromLine(ReadOnlySpan<byte> line, Span<int> lineNumbers)
{
int n = 0;
int i = 0;
while (i < line.Length)
{
int num;
byte c = line[i++];
if (c == '-')
{
num = '0' - line[i++];
while ((c = line[i++]) > ' ')
num = num * 10 + '0' - c;
}
else
{
num = c - '0';
while ((c = line[i++]) > ' ')
num = num * 10 + c - '0';
}
lineNumbers[n++] = num;
}
return n;
}
private static Vector256<int> RotateLeftOneByte(Vector256<int> vec) =>
Avx2.PermuteVar8x32(vec, Vector256.Create(1, 2, 3, 4, 5, 6, 7, 0));
private static Vector256<int> MoveLastByteAcross(Vector256<int> vec1, Vector256<int> vec2) =>
Avx2.Blend(vec1, vec2, 0b1000_0000);
private static bool FirstNBytesAreZero(Vector256<int> vec, int bytes)
{
uint zeroBitIndexes = Vector256.Equals(vec, Vector256<int>.Zero).ExtractMostSignificantBits();
uint mask = (uint)((1 << bytes) - 1);
return (zeroBitIndexes & mask) == mask;
}
}