-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day04.cs
48 lines (38 loc) · 1.26 KB
/
Day04.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
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2022.Solvers;
public class Day04 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int inputCursor = 0;
int part1 = 0;
int part2 = 0;
while (inputCursor < input.Length)
{
int start1 = ReadIntegerUntil(input, '-', ref inputCursor);
int end1 = ReadIntegerUntil(input, ',', ref inputCursor);
int start2 = ReadIntegerUntil(input, '-', ref inputCursor);
int end2 = ReadIntegerUntil(input, '\n', ref inputCursor);
if (start2 <= end1 && start1 <= end2)
{
part2++;
if ((start1 <= start2 && end2 <= end1) || (start2 <= start1 && end1 <= end2))
{
part1++;
}
}
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
public static int ReadIntegerUntil(ReadOnlySpan<byte> span, char c, ref int i)
{
// Assume that the first character is always a digit
int ret = span[i++] - '0';
byte cur;
while ((cur = span[i++]) != c)
ret = ret * 10 + (cur - '0');
return ret;
}
}