-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day13.cs
136 lines (107 loc) · 4.08 KB
/
Day13.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
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace AdventOfCode.CSharp.Y2021.Solvers;
public class Day13 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int foldsIndex = input.IndexOf((byte)'f');
ReadOnlySpan<byte> foldsInput = input.Slice(foldsIndex);
ReadOnlySpan<byte> dotsInput = input.Slice(0, foldsIndex - 1);
Span<int> xFolds = stackalloc int[32];
Span<int> yFolds = stackalloc int[32];
ParseFolds(foldsInput, xFolds, yFolds, out int numXFolds, out int numYFolds, out bool firstFoldIsX);
int firstFoldAxis = firstFoldIsX ? xFolds[0] : yFolds[0];
int maxX = xFolds[0] * 2 + 1;
int maxY = yFolds[0] * 2 + 1;
Span<byte> finalXPositions = stackalloc byte[maxX + 1];
Span<byte> finalYPositions = stackalloc byte[maxY + 1];
for (byte x = 0; x < 40; x++)
finalXPositions[x] = x;
for (int i = numXFolds - 1; i >= 0; i--)
{
int fold = xFolds[i];
Span<byte> dst = finalXPositions.Slice(fold + 1, fold);
finalXPositions.Slice(0, fold).CopyTo(dst);
dst.Reverse();
}
for (byte y = 0; y < 6; y++)
finalYPositions[y] = y;
for (int i = numYFolds - 1; i >= 0; i--)
{
int fold = yFolds[i];
Span<byte> dst = finalYPositions.Slice(fold + 1, fold);
finalYPositions.Slice(0, fold).CopyTo(dst);
dst.Reverse();
}
Span<int> letterMasks = stackalloc int[8];
var dotsAfterOneFold = new HashSet<int>();
int dotsInputCursor = 0;
while (dotsInputCursor < dotsInput.Length)
{
ParseDot(dotsInput, ref dotsInputCursor, out int x, out int y);
if (firstFoldIsX)
{
if (x > firstFoldAxis)
x = 2 * firstFoldAxis - x;
}
else
{
if (y > firstFoldAxis)
y = 2 * firstFoldAxis - y;
}
if (dotsAfterOneFold.Add((x << 16) | y))
{
x = finalXPositions[x];
y = finalYPositions[y];
(int letter, int col) = Math.DivRem(x, 5);
letterMasks[letter] |= 1 << (29 - (y * 5 + col));
}
}
solution.SubmitPart1(dotsAfterOneFold.Count);
Span<char> letters = stackalloc char[8];
for (int i = 0; i < 8; i++)
letters[i] = OCR.MaskToLetter(letterMasks[i]);
solution.SubmitPart2(letters);
}
private static void ParseFolds(ReadOnlySpan<byte> foldsInput, Span<int> xfolds, Span<int> yFolds, out int numXFolds, out int numYFolds, out bool firstFoldIsX)
{
numXFolds = 0;
numYFolds = 0;
bool isFirstFold = true;
firstFoldIsX = false;
int foldInputCursor = 0;
while (foldInputCursor < foldsInput.Length)
{
foldInputCursor += "fold along ".Length;
bool isX = foldsInput[foldInputCursor] == 'x';
foldInputCursor += "x=".Length;
int axis = foldsInput[foldInputCursor++] - '0';
byte c;
while ((c = foldsInput[foldInputCursor++]) != '\n')
axis = axis * 10 + (c - '0');
if (isFirstFold)
{
firstFoldIsX = isX;
isFirstFold = false;
}
if (isX)
xfolds[numXFolds++] = axis;
else
yFolds[numYFolds++] = axis;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void ParseDot(ReadOnlySpan<byte> dotsInput, ref int dotsInputCursor, out int x, out int y)
{
x = dotsInput[dotsInputCursor++] - '0';
byte c;
while ((c = dotsInput[dotsInputCursor++]) != ',')
x = x * 10 + (c - '0');
y = dotsInput[dotsInputCursor++] - '0';
while ((c = dotsInput[dotsInputCursor++]) != '\n')
y = y * 10 + (c - '0');
}
}