-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc-11.1.csx
86 lines (77 loc) · 2.11 KB
/
aoc-11.1.csx
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
using System.Linq;
var input = System.IO.File.ReadAllLines(@".\input.11.1.txt").ToArray();
var chunkType1 = "()";
var chunkType2 = "[]";
var chunkType3 = "{}";
var chunkType4 = "<>";
var validChunks = new HashSet<string>()
{
chunkType1,
chunkType2,
chunkType3,
chunkType4,
};
var openChunkChars = "([{<";
var closeChunkChars = ")]}>";
int errorSum = 0;
Stack<char> openChunks = new Stack<char>();
var incompleteLines = new List<string>();
foreach (var line in input)
{
bool corruptLine = false;
foreach (var lineChar in line)
{
if (openChunkChars.Contains(lineChar))
{
openChunks.Push(lineChar);
}
else
{
char lastOpenedChunk = openChunks.Pop();
string chunk = new (new char[]{ lastOpenedChunk, lineChar });
if (!validChunks.Contains(chunk))
{
corruptLine = true;
break;
}
}
}
if (!corruptLine) // Then it is incomplete
{
incompleteLines.Add(line);
}
}
long incompleteSum = 0;
List<long> incompleteSums = new ();
foreach (var incompleteLine in incompleteLines)
{
openChunks = new Stack<char>();
foreach (var lineChar in incompleteLine)
{
if (openChunkChars.Contains(lineChar))
{
openChunks.Push(lineChar);
}
else
{
char lastOpenedChunk = openChunks.Pop();
}
}
System.Console.WriteLine("chunks open :" + openChunks.Count());
long incompleteSumLine = 0;
foreach (var chunk in openChunks)
{
incompleteSumLine *= 5L;
switch (chunk)
{
case '(': incompleteSumLine += 1L; break;
case '[': incompleteSumLine += 2L; break;
case '{': incompleteSumLine += 3L; break;
case '<': incompleteSumLine += 4L; break;
}
}
System.Console.WriteLine($"Incomplete sum line: {incompleteSumLine}");
incompleteSums.Add(incompleteSumLine);
}
var array = incompleteSums.OrderBy(i => i).ToArray();
System.Console.WriteLine("middle sum: " + array[array.Length / 2]);