-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday7.linq
More file actions
137 lines (123 loc) · 2.53 KB
/
Copy pathday7.linq
File metadata and controls
137 lines (123 loc) · 2.53 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<Query Kind="Program" />
//#define TEST
void Main()
{
// Advent of code 2016
// Day 7
// http://adventofcode.com/2016/day/7
var input = GetInput();
var result = GetResult(input);
result.Dump("Result 1");
#if TEST
System.Diagnostics.Debug.Assert(2 == result, "Test result is wrong.");
#endif
}
public Tuple<int, int> GetResult(IEnumerable<string> input)
{
int resultTLS = 0, resultSSL = 0;
foreach (var line in input)
{
if (SupportsTLS(line))
{
resultTLS++;
}
if (SupportsSSL(line))
{
resultSSL++;
}
}
return new Tuple<int, int>(resultTLS, resultSSL);
}
public static string badpattern = @"\[[a-z]*(?<abba>(?<c1>[a-z])(?<c2>((?!\k<c1>)[a-z]))\k<c2>\k<c1>)[a-z]*\]";
public static string goodpattern = @"(?<c1>[a-z])(?<c2>((?!\k<c1>)[a-z]))\k<c2>\k<c1>";
public Regex bad_re = new Regex(badpattern);
public Regex good_re = new Regex(goodpattern);
public bool SupportsTLS(string input)
{
var m = bad_re.Match(input);
//m.Dump("Bad");
if (m.Success)
return false;
m = good_re.Match(input);
//m.Dump("Good");
return m.Success;
}
public bool SupportsSSL(string input)
{
var abas = new List<string>();
var hypernets = new List<string>();
Action<string> GetABAs = (str) =>
{
int index = 0;
char c1, c2, c3;
while (index < str.Length - 2)
{
c1 = str[index++];
c2 = str[index];
c3 = str[index + 1];
if ((c1 == c3) && (c1 != c2))
{
abas.Add($"{c1}{c2}{c3}");
}
}
};
Action<string> SplitInput = (str) =>
{
string current = "";
foreach (char c in str)
{
switch (c)
{
case '[':
if (!string.IsNullOrWhiteSpace(current))
{
GetABAs(current);
current = "";
}
break;
case ']':
if (!string.IsNullOrWhiteSpace(current))
{
hypernets.Add(current);
current = "";
}
break;
default:
current += c;
break;
}
}
// Assume [ and ] are balanced - therefore must be a 'supernet' sequence
if (!string.IsNullOrWhiteSpace(current))
{
GetABAs(current);
}
};
SplitInput(input);
var hyper_str = string.Join("|", hypernets);
foreach (var aba in abas)
{
string bab = $"{aba[1]}{aba[0]}{aba[1]}";
if (hyper_str.Contains(bab))
{
return true;
}
}
return false;
}
public IEnumerable<string> GetInput()
{
#if TEST
var result = new List<string>
{
"abba[mnop]qrst",
"abcd[bddb]xyyx",
"aaaa[qwer]tyui",
"ioxxoj[asdfgh]zxcvbn"
};
#else
string fname = Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), "day7-input.txt");
var result = File.ReadAllLines(fname);
#endif
return result;
}