my advent of code solutions
1namespace Solutions._2021;
2
3/// <summary>
4/// Day 10: <a href="https://adventofcode.com/2021/day/10"/>
5/// </summary>
6public sealed class Day10SyntaxScoring() : Day(2021, 10, "Syntax Scoring")
7{
8 private static readonly Dictionary<char, char> MatchedBrackets = new()
9 {
10 {'(', ')'},
11 {'[', ']'},
12 {'{', '}'},
13 {'<', '>'},
14 };
15
16 private static readonly Dictionary<char, long> Scores = new()
17 {
18 { ')', 3 },
19 { ']', 57 },
20 { '}', 1197 },
21 { '>', 25137 },
22 };
23
24 private static readonly Dictionary<char, long> ScoresPart2 = new()
25 {
26 { '(', 1 },
27 { '[', 2 },
28 { '{', 3 },
29 { '<', 4 },
30 };
31
32 private readonly List<long> _scores2 = [];
33
34 private long _score1;
35
36 public override void ProcessInput()
37 {
38 _score1 = 0L;
39 foreach (var line in Input)
40 {
41 var corrupt = false;
42 var s = new Stack<char>();
43
44 foreach (var c in line)
45 {
46 if (ScoresPart2.ContainsKey(c))
47 {
48 s.Push(c);
49 }
50 else
51 {
52 if (c == MatchedBrackets[s.Pop()]) continue;
53 _score1 += Scores[c];
54 corrupt = true;
55 break;
56 }
57 }
58
59 if (corrupt) continue;
60 var score2 = 0L;
61 while (s.Count != 0)
62 {
63 score2 *= 5;
64 score2 += ScoresPart2[s.Pop()];
65 }
66
67 _scores2.Add(score2);
68 }
69 }
70
71 public override object Part1() => _score1;
72
73 public override object Part2()
74 {
75 var sorted = _scores2.OrderBy(i => i).ToList();
76 return sorted[sorted.Count / 2];
77 }
78}