-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay02.cs
51 lines (43 loc) · 1.34 KB
/
Day02.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
using System;
using System.Runtime.CompilerServices;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2020.Solvers;
public class Day02 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int part1 = 0;
int part2 = 0;
foreach (Range lineRange in input.SplitLines())
{
ReadOnlySpan<byte> line = input[lineRange];
int i = 0;
int left = ParsePosInt(line, until: '-', ref i);
int right = ParsePosInt(line, until: ' ', ref i);
byte letter = line[i];
ReadOnlySpan<byte> password = line.Slice(i + 3);
int letterCount = password.Count(letter);
if (left <= letterCount && letterCount <= right)
{
part1++;
}
if (password[left - 1] == letter ^ password[right - 1] == letter)
{
part2++;
}
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int ParsePosInt(ReadOnlySpan<byte> str, char until, ref int i)
{
byte c = str[i++];
int num = c - '0';
while ((c = str[i++]) != until)
{
num = num * 10 + (c - '0');
}
return num;
}
}