-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.cs
168 lines (145 loc) · 3.96 KB
/
Day10.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
namespace AdventOfCode2021;
public static class Day10
{
private static readonly Dictionary<char, int> ErrorScoreMap = new Dictionary<char, int>
{
{ ')', 3 },
{ ']', 57 },
{ '}', 1197 },
{ '>', 25137 }
};
private static readonly Dictionary<char, ulong> AutoCompleteScoreMap = new Dictionary<char, ulong>
{
{ ')', 1 },
{ ']', 2 },
{ '}', 3 },
{ '>', 4 }
};
private static readonly Dictionary<char, char> SymbolPairs = new Dictionary<char, char>
{
{ '(', ')' },
{ ')', '(' },
{ '[', ']' },
{ ']', '[' },
{ '{', '}' },
{ '}', '{' },
{ '<', '>' },
{ '>', '<' }
};
private static readonly HashSet<char> OpenSymbols = new HashSet<char>
{
'(',
'[',
'{',
'<'
};
private static readonly HashSet<char> CloseSymbols = new HashSet<char>
{
')',
']',
'}',
'>'
};
public static void Part1()
{
string[] inputs = InputHelper.GetInput(10);
int totalSyntaxErrorScore = 0;
for (int i = 0; i < inputs.Length; i++)
{
string line = inputs[i];
totalSyntaxErrorScore += GetSyntaxErrorScore(line);
}
Console.WriteLine(totalSyntaxErrorScore);
}
private static int GetSyntaxErrorScore(string line)
{
Stack<char> groups = new Stack<char>();
for (int j = 0; j < line.Length; j++)
{
char symbol = line[j];
if (IsOpenSymbol(symbol))
{
// new level of nesting
groups.Push(symbol);
}
else
{
// close nesting
if (groups.TryPeek(out char prevSymbol))
{
if (SymbolPairs[prevSymbol] == symbol)
{
groups.Pop();
}
else
{
return ErrorScoreMap[symbol];
}
}
}
}
return 0;
}
private static bool IsOpenSymbol(char symbol)
{
return OpenSymbols.Contains(symbol);
}
private static bool IsCloseSymbol(char symbol)
{
return CloseSymbols.Contains(symbol);
}
public static void Part2()
{
string[] inputs = InputHelper.GetInput(10);
List<ulong> autoCompleteScores = new List<ulong>();
for (int i = 0; i < inputs.Length; i++)
{
string line = inputs[i];
ulong score = GetAutoCompleteScore(line);
if (score == 0)
{
continue;
}
autoCompleteScores.Add(score);
}
autoCompleteScores.Sort();
Console.WriteLine(autoCompleteScores[autoCompleteScores.Count / 2]);
}
private static ulong GetAutoCompleteScore(string line)
{
Stack<char> groups = new Stack<char>();
for (int j = 0; j < line.Length; j++)
{
char symbol = line[j];
if (IsOpenSymbol(symbol))
{
// new level of nesting
groups.Push(symbol);
}
else
{
// close nesting
if (groups.TryPeek(out char prevSymbol))
{
if (SymbolPairs[prevSymbol] == symbol)
{
groups.Pop();
}
else
{
// Corrupted
return 0;
}
}
}
}
IEnumerable<ulong> scores = groups
.Select(symbol => AutoCompleteScoreMap[SymbolPairs[symbol]]);
ulong totalScore = 0;
foreach (ulong score in scores)
{
totalScore = 5u * totalScore + score;
}
return totalScore;
}
}