Esempio n. 1
0
        static long First(string inputFile)
        {
            var lines      = new LineParser().ReadData(inputFile);
            var errorScore = 0L;

            foreach (var line in lines)
            {
                var stack = new Stack <char>();
                foreach (var c in line)
                {
                    if (ChunkPairs.IsOpenCharacter(c))
                    {
                        stack.Push(c);
                    }
                    if (ChunkPairs.IsCloseCharacter(c))
                    {
                        var pair = ChunkPairs.FindByCloseCharacter(c);

                        if (stack.Peek() != pair.OpenCharacter)
                        {
                            //syntax error
                            errorScore += pair.SyntaxErrorScore;
                            break;
                        }
                        stack.Pop();
                    }
                }
            }
            return(errorScore);
        }
Esempio n. 2
0
        static long Second(string inputFile)
        {
            var lines = new LineParser().ReadData(inputFile);
            //var errorLines = new List<string>();
            var completionScores = new List <long>();

            foreach (var line in lines)
            {
                var completionScore = 0L;
                var stack           = new Stack <char>();
                var syntaxError     = false;
                foreach (var c in line)
                {
                    if (ChunkPairs.IsOpenCharacter(c))
                    {
                        stack.Push(c);
                    }
                    if (ChunkPairs.IsCloseCharacter(c))
                    {
                        var pair = ChunkPairs.FindByCloseCharacter(c);

                        if (stack.Peek() != pair.OpenCharacter)
                        {
                            //syntax error
                            syntaxError = true;
                            break;
                        }
                        stack.Pop();
                    }
                }
                while (!syntaxError && stack.Count > 0)
                {
                    var charToClose = stack.Pop();
                    var pair        = ChunkPairs.FindByOpenCharacter(charToClose);
                    completionScore *= 5;
                    completionScore += pair.CompletionScore;
                }
                if (completionScore > 0)
                {
                    completionScores.Add(completionScore);
                }
            }
            return(completionScores.OrderBy(l => l).ElementAt(completionScores.Count / 2));
        }