-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay06.cs
61 lines (51 loc) · 1.54 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
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2016.Solvers;
public class Day06 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int width = input.IndexOf((byte)'\n');
int[,] counts = new int[width + 1, 26];
int i = 0;
while (i < input.Length)
{
for (int c = 0; c < width; c++)
{
int letter = input[i++] - 'a';
counts[c, letter]++;
}
i++; // skip newline character
}
char[] part1 = new char[width];
char[] part2 = new char[width];
for (int c = 0; c < width; c++)
{
int minLetter = -1;
int minCount = int.MaxValue;
int maxLetter = -1;
int maxCount = 0;
for (int letter = 0; letter < 26; letter++)
{
int count = counts[c, letter];
if (count > 0)
{
if (count < minCount)
{
minCount = count;
minLetter = letter;
}
if (count > maxCount)
{
maxCount = count;
maxLetter = letter;
}
}
}
part1[c] = (char)('a' + maxLetter);
part2[c] = (char)('a' + minLetter);
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
}