-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.cs
91 lines (79 loc) · 2.47 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
public class Day10
{
public static async Task<string> Part1Async(bool example = false)
{
var data = await ParseInput(example);
var score = data.Select(l => getCorruptChar(l)).Select(c => GetScore(c));
return score.Sum().ToString();
}
public static async Task<string> Part2Async(bool example = false)
{
var data = await ParseInput(example);
var score = data.Select(l => returnCompleteLineScore(l)).Where(s => s != 0).OrderBy(s => s).ToList();
return score.Skip(score.Count / 2).Take(1).Single().ToString();
}
private static int GetScore(char c)
{
return c switch
{
')' => 3,
']' => 57,
'}' => 1197,
'>' => 25137,
_ => 0
};
}
private static readonly char[] openChars = new char[] { '(', '[', '{', '<' };
private static readonly char[] closeChars = new char[] { ')', ']', '}', '>' };
private static char getCorruptChar(List<char> line)
{
var stack = new Stack<char>();
foreach (var c in line)
{
if (openChars.Contains(c))
{
stack.Push(c);
}
else
{
var popC = stack.Pop();
if (closeChars[Array.IndexOf(openChars, popC)] != c)
return c;
}
}
return ' ';
}
private static long returnCompleteLineScore(List<char> line)
{
var stack = new Stack<char>();
foreach (var c in line)
{
if (openChars.Contains(c))
{
stack.Push(c);
}
else
{
var popC = stack.Pop();
if (closeChars[Array.IndexOf(openChars, popC)] != c)
return 0;
}
}
long score = 0;
//if stuff is left on the stack, complete te line
while (stack.TryPop(out char popC))
{
var index = Array.IndexOf(openChars, popC);
Console.Write(closeChars[index]);
score = (score * 5) + (index + 1);
}
Console.WriteLine($" {score}");
return score;
}
private static async Task<List<List<char>>> ParseInput(bool example)
{
var input = await File.ReadAllLinesAsync($"inputs/day10{(example ? ".example" : string.Empty)}.txt");
var data = input.Select(line => line.ToCharArray().ToList()).ToList();
return data;
}
}