-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay07.cs
94 lines (84 loc) · 2.88 KB
/
Day07.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2016.Solvers;
public class Day07 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int part1 = 0;
int part2 = 0;
byte[] abas = new byte[26 * 26];
byte[] babs = new byte[26 * 26];
foreach (Range lineRange in input.SplitLines())
{
ReadOnlySpan<byte> line = input[lineRange];
Array.Clear(abas, 0, abas.Length);
Array.Clear(babs, 0, babs.Length);
bool hasAbbaOutsideHypernet = false;
bool hasAbbaInsideHypernet = false;
bool supportsSSL = false;
// c1, c2, c3, c4 defines a sliding window of 4 characters across the line
byte c1 = line[0], c2 = line[1], c3 = line[2];
bool insideHypernet = false;
for (int i = 3; i <= line.Length; i++)
{
byte c4 = i == line.Length ? (byte)'\0' : line[i];
if (c1 == '[')
{
insideHypernet = true;
}
else if (c1 == ']')
{
insideHypernet = false;
}
else
{
if (c1 == c4 && c2 == c3 && c2 != '[' && c1 != c2) // found an ABBA
{
if (insideHypernet)
{
hasAbbaInsideHypernet = true;
}
else
{
hasAbbaOutsideHypernet = true;
}
}
if (c1 == c3 && c2 != c1 && c2 != '[' && c2 != ']')
{
if (insideHypernet)
{
babs[(c1 - 'a') * 26 + (c2 - 'a')] = 1;
if (abas[(c2 - 'a') * 26 + (c1 - 'a')] == 1)
{
supportsSSL = true;
}
}
else
{
abas[(c1 - 'a') * 26 + (c2 - 'a')] = 1;
if (babs[(c2 - 'a') * 26 + (c1 - 'a')] == 1)
{
supportsSSL = true;
}
}
}
}
// slide the window down
c1 = c2;
c2 = c3;
c3 = c4;
}
if (hasAbbaOutsideHypernet && !hasAbbaInsideHypernet)
{
part1 += 1;
}
if (supportsSSL)
{
part2 += 1;
}
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
}