-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day12.cs
165 lines (138 loc) · 6.04 KB
/
Day12.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
using System;
using System.Numerics;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day12 : ISolver
{
// TODO: Investigate a faster algorithm, one that could theoretically scale to 1 million copies
// In the meantime I have this which runs in 2ms but I think I can do a lot better.
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
// Enough to fit a list of 24 springs per line
Span<byte> springs = stackalloc byte[128];
// Enough to fit 9 contiguous groups per line
Span<int> contiguousGroups = stackalloc int[48];
long part1 = 0;
long part2 = 0;
long[][] waysCache = new long[128][];
for (int i = 0; i < waysCache.Length; i++)
waysCache[i] = new long[48];
ulong[] nonZeroCounts = new ulong[128];
while (input.Length > 0)
{
int lineSpringsEnd = input.IndexOf((byte)' ');
ReadOnlySpan<byte> lineSprings = input.Slice(0, lineSpringsEnd);
input = input.Slice(lineSpringsEnd + 1);
int numGroups = 0;
int num = input[0] - '0';
int index = 1;
while (true)
{
byte c = input[index++];
if (c >= '0')
{
num = 10 * num + c - '0';
}
else
{
contiguousGroups[numGroups++] = num;
if (c == '\n')
break;
num = input[index++] - '0';
}
}
input = input.Slice(index);
int newSpringLen = 5 * lineSprings.Length + 4;
int newNumGroups = 5 * numGroups;
for (int i = 0; i < newSpringLen; i += lineSprings.Length + 1)
{
lineSprings.CopyTo(springs.Slice(i));
if (i + lineSprings.Length != newSpringLen)
springs[i + lineSprings.Length] = (byte)'?';
}
for (int i = numGroups; i < newNumGroups; i += numGroups)
contiguousGroups.Slice(0, numGroups).CopyTo(contiguousGroups.Slice(i, numGroups));
CountWays(springs.Slice(0, newSpringLen), contiguousGroups.Slice(0, newNumGroups), waysCache, nonZeroCounts, ref part1, ref part2);
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static void CountWays(ReadOnlySpan<byte> springs, ReadOnlySpan<int> contiguousGroups, long[][] waysCache, ulong[] nonZeroCounts, ref long part1, ref long part2)
{
for (int i = 0; i <= springs.Length; i++)
waysCache[i].AsSpan().Slice(0, contiguousGroups.Length + 1).Clear();
nonZeroCounts.AsSpan().Slice(0, springs.Length + 1).Clear();
waysCache[springs.Length][contiguousGroups.Length] = 1;
nonZeroCounts[springs.Length] |= 1UL << contiguousGroups.Length;
byte maxGroupIndex = 0;
byte[] maxGroupsPerSpring = new byte[springs.Length + 1];
for (int i = 1; i <= springs.Length; i++)
{
byte c = springs[springs.Length - i];
if (maxGroupIndex < contiguousGroups.Length && (c is (byte)'#' or (byte)'?'))
{
bool matchesNum = true;
int groupSize = contiguousGroups[contiguousGroups.Length - maxGroupIndex - 1] - 1;
for (int j = 0; j < groupSize; j++)
{
maxGroupsPerSpring[i++] = maxGroupIndex;
c = springs[springs.Length - i];
if (c == '.')
{
matchesNum = false;
break;
}
}
if (matchesNum)
{
// can't start when adjacent to broken
while (i < springs.Length && springs[springs.Length - i - 1] == '#')
maxGroupsPerSpring[i++] = maxGroupIndex;
maxGroupIndex++;
if (i < springs.Length)
maxGroupsPerSpring[i++] = maxGroupIndex;
}
}
if (i <= springs.Length)
maxGroupsPerSpring[i] = maxGroupIndex;
}
int endingSprings = springs.Length - springs.LastIndexOf((byte)'#') - 1;
for (int i = springs.Length; i >= 0; i--)
{
long[] groupCache = waysCache[i];
ulong nonZeroCount = nonZeroCounts[i] & ~1UL & ((1UL << (maxGroupsPerSpring[i] + 1)) - 1);
while (nonZeroCount != 0)
{
ulong t = nonZeroCount & (~nonZeroCount + 1);
int j = BitOperations.TrailingZeroCount(t);
byte currentSpring = springs[springs.Length - i];
if (currentSpring != '#')
{
waysCache[i - 1][j] += groupCache[j];
nonZeroCounts[i - 1] |= 1UL << j;
}
if (currentSpring != '.')
{
int nextGroupSize = contiguousGroups[contiguousGroups.Length - j];
if (!springs.Slice(springs.Length - i + 1, nextGroupSize - 1).Contains((byte)'.'))
{
if (j == 1)
{
waysCache[i - nextGroupSize][j - 1] += groupCache[j];
nonZeroCounts[i - nextGroupSize] |= 1UL << (j - 1);
}
else if (springs[springs.Length - i + nextGroupSize] != '#')
{
waysCache[i - nextGroupSize - 1][j - 1] += groupCache[j];
nonZeroCounts[i - nextGroupSize - 1] |= 1UL << (j - 1);
}
}
}
nonZeroCount ^= t;
}
}
part1 += waysCache[4 * (springs.Length / 5) + 3][4 * (contiguousGroups.Length / 5)];
for (int i = endingSprings; i >= 0; i--)
part2 += waysCache[i][0];
}
}