diff --git a/aoc2021.test/DayTests.cs b/aoc2021.test/DayTests.cs
index 51cee5a..60b83d1 100644
--- a/aoc2021.test/DayTests.cs
+++ b/aoc2021.test/DayTests.cs
@@ -13,7 +13,7 @@ public class DayTests
[DataRow(typeof(Day07), "345035", "97038163")]
[DataRow(typeof(Day08), "362", "1020159")]
[DataRow(typeof(Day09), "478", "1327014")]
- [DataRow(typeof(Day10), "288291", "")]
+ [DataRow(typeof(Day10), "288291", "820045242")]
public void CheckAllDays(Type dayType, string part1, string part2)
{
var s = Stopwatch.StartNew();
diff --git a/aoc2021/Day10.cs b/aoc2021/Day10.cs
index 4907a80..bd93c77 100644
--- a/aoc2021/Day10.cs
+++ b/aoc2021/Day10.cs
@@ -1,17 +1,19 @@
-using System.Collections;
-
-namespace aoc2021;
+namespace aoc2021;
///
/// Day 10:
///
public sealed class Day10 : Day
{
- private readonly List _input;
- private readonly char[] _openers = { '(', '[', '{', '<' };
- private readonly char[] _closers = { ')', ']', '}', '>' };
+ private static readonly Dictionary MatchedBrackets = new()
+ {
+ {'(', ')'},
+ {'[', ']'},
+ {'{', '}'},
+ {'<', '>'}
+ };
- private readonly Dictionary _scores = new()
+ private static readonly Dictionary Scores = new()
{
{ ')', 3 },
{ ']', 57 },
@@ -19,45 +21,57 @@ public sealed class Day10 : Day
{ '>', 25137 }
};
- private readonly Dictionary _scoresClosing = new()
+ private static readonly Dictionary ScoresPart2 = new()
{
{ '(', 1 },
{ '[', 2 },
{ '{', 3 },
{ '<', 4 }
};
-
+
+ private readonly long _score1;
+ private readonly List _scores2 = new();
+
public Day10() : base(10, "Syntax Scoring")
{
- _input = Input.ToList();
- }
-
- public override string Part1()
- {
- var s = new Stack();
- var score = 0;
- foreach (var line in _input)
+ _score1 = 0L;
+ foreach (var line in Input)
{
+ var corrupt = false;
+ var s = new Stack();
+
foreach (var c in line)
{
- if (_openers.Contains(c))
+ if (ScoresPart2.ContainsKey(c))
{
s.Push(c);
}
else
{
- var y = s.Pop();
- if (Math.Abs(y - c) <= 3) continue;
- score += _scores[c];
+ if (c == MatchedBrackets[s.Pop()]) continue;
+ _score1 += Scores[c];
+ corrupt = true;
break;
}
}
- }
- return score.ToString();
+ if (corrupt) continue;
+ var score2 = 0L;
+ while (s.Any())
+ {
+ score2 *= 5;
+ score2 += ScoresPart2[s.Pop()];
+ }
+
+ _scores2.Add(score2);
+ }
}
+ public override string Part1() => $"{_score1}";
+
public override string Part2()
{
+ var sorted = _scores2.OrderBy(i => i).ToList();
+ return $"{sorted[sorted.Count / 2]}";
}
}