-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10Task2.cs
More file actions
39 lines (36 loc) · 1004 Bytes
/
Copy pathDay10Task2.cs
File metadata and controls
39 lines (36 loc) · 1004 Bytes
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
namespace AdventOfCode2021;
public class Day10Task2 : Day10Task1
{
protected static readonly Dictionary<char, int> pointsPerCharacter = new Dictionary<char, int>
{
['('] = 1,
['['] = 2,
['{'] = 3,
['<'] = 4,
};
public long FillLine(string line)
{
var stack = new Stack<char>();
foreach(var c in line)
{
if (closingCharacter.ContainsKey(c))
stack.Push(c);
else if (stack.Count == 0)
return 0;
else if (c == closingCharacter[stack.Peek()])
stack.Pop();
else return 0;
}
var result = 0L;
foreach(var c in stack)
{
result = result * 5 + pointsPerCharacter[c];
}
return result;
}
public long FindMiddleFillLine(string[] input)
{
var list = input.Select(FillLine).Where(x => x > 0).OrderBy(x => x).ToList();
return list[list.Count / 2];
}
}