using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace _2021___Day_10 { class Program { static void Main(string[] args) { var input = File.ReadAllLines(@"../../../../Input.in"); var result = 0; var scores = new Dictionary<char, int> { {')', 3 }, {']', 57 }, {'}', 1197 }, {'>', 25137 } }; var starters = new char[] { '(', '[', '{', '<' }; foreach (var line in input) { var curStart = new Stack<char>(); foreach (var n in line) { if (starters.Any(x => x == n)) { curStart.Push(n); } else { var tester = curStart.Pop(); if (Math.Abs(tester - n) >= 3) { result += scores[n]; } } } } Console.WriteLine("Checker result is: " + result.ToString()); var otherScores = new Dictionary<char, int> { {'(', 1 }, {'[', 2 }, {'{', 3 }, {'<', 4 } }; var finalScores = new List<long>(); foreach (var line in input) { var curStart = new Stack<char>(); var correct = true; foreach (var n in line) { if (starters.Any(x => x == n)) { curStart.Push(n); } else { var tester = curStart.Pop(); if (Math.Abs(tester - n) >= 3) { correct = false; break; } } } if (correct) { var curScore = 0L; while (curStart.Count() > 0) { curScore *= 5; curScore += otherScores[curStart.Pop()]; } finalScores.Add(curScore); } } finalScores = finalScores.OrderBy(i => i).ToList(); var middleScore = (finalScores[(finalScores.Count() - 1) / 2]); Console.WriteLine("The middle score after completing lines is : " + middleScore.ToString()); } } }