-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day06.cs
70 lines (56 loc) · 1.58 KB
/
Day06.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
using AdventOfCode.CSharp.Common;
using System;
using System.Runtime.CompilerServices;
namespace AdventOfCode.CSharp.Y2021.Solvers;
public class Day06 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
Span<long> counts = stackalloc long[9];
counts.Clear();
int cursor = 0;
while (cursor < input.Length)
{
counts[input[cursor] - '0']++;
cursor += 2;
}
Iterate(counts, 80);
long part1 = 0;
foreach (long count in counts)
part1 += count;
solution.SubmitPart1(part1);
Iterate(counts, 256 - 80);
long part2 = 0;
foreach (long count in counts)
part2 += count;
solution.SubmitPart2(part2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Iterate(Span<long> counts, int n)
{
int i = 0;
while (i + 7 < n)
{
long sevens = counts[7];
counts[7] = counts[5];
counts[5] += counts[3];
counts[3] += counts[1];
counts[1] += counts[8];
counts[8] = counts[6];
counts[6] += counts[4];
counts[4] += counts[2];
counts[2] += counts[0];
counts[0] += sevens;
i += 7;
}
while (i < n)
{
long zeroes = counts[0];
for (int j = 0; j < 8; j++)
counts[j] = counts[j + 1];
counts[6] += zeroes;
counts[8] = zeroes;
i++;
}
}
}