about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2021-12-10 12:49:49 -0500
committerBen Harris <ben@tilde.team>2021-12-10 12:49:49 -0500
commit682d0545aacacd32d4eb61af87ca04ae1b54eba9 (patch)
tree1860af05e0b808bcba856bd5dc07367a908d0579
parentcdf3674e4e837b6646f6367a6b3e9371a49120e1 (diff)
d10p2
-rw-r--r--aoc2021.test/DayTests.cs2
-rw-r--r--aoc2021/Day10.cs60
2 files changed, 38 insertions, 24 deletions
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;
 
 /// <summary>
 /// Day 10: <see href="https://adventofcode.com/2021/day/10"/>
 /// </summary>
 public sealed class Day10 : Day
 {
-    private readonly List<string> _input;
-    private readonly char[] _openers = { '(', '[', '{', '<' };
-    private readonly char[] _closers = { ')', ']', '}', '>' };
+    private static readonly Dictionary<char, char> MatchedBrackets = new()
+    {
+        {'(', ')'},
+        {'[', ']'},
+        {'{', '}'},
+        {'<', '>'}
+    };
 
-    private readonly Dictionary<char, int> _scores = new()
+    private static readonly Dictionary<char, long> Scores = new()
     {
         { ')', 3 },
         { ']', 57 },
@@ -19,45 +21,57 @@ public sealed class Day10 : Day
         { '>', 25137 }
     };
 
-    private readonly Dictionary<char, int> _scoresClosing = new()
+    private static readonly Dictionary<char, long> ScoresPart2 = new()
     {
         { '(', 1 },
         { '[', 2 },
         { '{', 3 },
         { '<', 4 }
     };
-    
-    public Day10() : base(10, "Syntax Scoring")
-    {
-        _input = Input.ToList();
-    }
 
-    public override string Part1()
+    private readonly long _score1;
+    private readonly List<long> _scores2 = new();
+
+    public Day10() : base(10, "Syntax Scoring")
     {
-        var s = new Stack<char>();
-        var score = 0;
-        foreach (var line in _input)
+        _score1 = 0L;
+        foreach (var line in Input)
         {
+            var corrupt = false;
+            var s = new Stack<char>();
+
             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]}";
     }
 }