-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day08.cs
70 lines (61 loc) · 1.6 KB
/
Day08.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
62
63
64
65
66
67
68
69
70
using AdventOfCode.CSharp.Common;
using System;
namespace AdventOfCode.CSharp.Y2015.Solvers;
public class Day08 : 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];
part1 += GetPart1Length(line);
part2 += GetPart2Length(line);
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static int GetPart1Length(ReadOnlySpan<byte> line)
{
int charCount = 0;
int i = 1;
while (i < line.Length - 1)
{
charCount++;
if (line[i] == '\\')
{
switch (line[i + 1])
{
case (byte)'\\':
case (byte)'\"':
i += 2;
break;
case (byte)'x':
i += 4;
break;
default:
i++;
break;
}
}
else
{
i++;
}
}
return line.Length - charCount;
}
private static int GetPart2Length(ReadOnlySpan<byte> line)
{
int charCount = 2 + line.Length;
foreach (byte c in line)
{
if (c is (byte)'\\' or (byte)'\"')
{
charCount++;
}
}
return charCount - line.Length;
}
}