-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay06.cs
49 lines (43 loc) · 1.3 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
using System;
using System.Numerics;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2020.Solvers;
public class Day06 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int part1 = 0;
int part2 = 0;
uint personAnswers = 0;
uint groupAnswers = 0;
uint groupAllAnswers = uint.MaxValue;
foreach (byte c in input)
{
if (c == '\n')
{
if (personAnswers == 0)
{
part1 += BitOperations.PopCount(groupAnswers);
part2 += BitOperations.PopCount(groupAllAnswers);
groupAnswers = 0;
groupAllAnswers = uint.MaxValue;
}
else
{
groupAnswers |= personAnswers;
groupAllAnswers &= personAnswers;
personAnswers = 0;
}
}
else
{
personAnswers |= 1u << (c - 'a');
}
}
// process the last group
part1 += BitOperations.PopCount(groupAnswers);
part2 += BitOperations.PopCount(groupAllAnswers);
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
}