-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day03.cs
61 lines (50 loc) · 1.8 KB
/
Day03.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 System.Numerics;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2022.Solvers;
public class Day03 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int part1 = 0;
int part2 = 0;
while (input.Length > 0)
{
ReadOnlySpan<byte> sack1 = ReadSack(ref input);
ReadOnlySpan<byte> sack2 = ReadSack(ref input);
ReadOnlySpan<byte> sack3 = ReadSack(ref input);
part1 += GetPriority(sack1, out ulong items1) + GetPriority(sack2, out ulong items2) + GetPriority(sack3, out ulong items3);
part2 += ExtractPriorityFromBitSet(items1 & items2 & items3);
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static ReadOnlySpan<byte> ReadSack(ref ReadOnlySpan<byte> input)
{
int sackEndIndex = input.IndexOf((byte)'\n');
ReadOnlySpan<byte> sack = input[..sackEndIndex];
input = input[(sackEndIndex + 1)..];
return sack;
}
private static int GetPriority(ReadOnlySpan<byte> rucksack, out ulong sackItems)
{
int halfSize = rucksack.Length / 2;
ulong half1 = 0;
for (int i = 0; i < halfSize; i++)
half1 |= 1UL << (rucksack[i] - 'A');
ulong half2 = 0;
for (int i = halfSize; i < 2 * halfSize; i++)
half2 |= 1UL << (rucksack[i] - 'A');
ulong common = half1 & half2;
sackItems = half1 | half2;
return ExtractPriorityFromBitSet(common);
}
private static int ExtractPriorityFromBitSet(ulong bits)
{
int commonIndex = BitOperations.TrailingZeroCount(bits);
if (commonIndex < 26)
return commonIndex + 27;
else
return commonIndex - 31;
}
}