Exemple #1
0
        public static void Part_1(string input, bool pretty)
        {
            c_input_reader input_reader = new c_input_reader(input);

            int result = 0;

            while (input_reader.has_more_lines())
            {
                string input_line = input_reader.read_line();

                Stack <c_chunk> chunks = new Stack <c_chunk>();

                bool line_corrupt = false;

                foreach (char input_char in input_line)
                {
                    if (input_char == '(' ||
                        input_char == '[' ||
                        input_char == '{' ||
                        input_char == '<')
                    {
                        chunks.Push(new c_chunk(input_char));
                    }
                    else
                    {
                        c_chunk chunk = chunks.Pop();

                        if (input_char != chunk.end_char)
                        {
                            int score = get_score(input_char);

                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine(
                                "Expected {0}, but found {1} instead. ({2} points)",
                                chunk.end_char,
                                input_char,
                                score);

                            result += score;

                            line_corrupt = true;
                            break;
                        }
                    }
                }

                if (!line_corrupt)
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.WriteLine("Line valid.");
                }
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine();
            Console.WriteLine("Result = {0}", result);
            Console.ResetColor();
        }
Exemple #2
0
        public static void Part_2(string input, bool pretty)
        {
            c_input_reader input_reader = new c_input_reader(input);

            List <UInt64> scores = new List <UInt64>();

            while (input_reader.has_more_lines())
            {
                string input_line = input_reader.read_line();

                Stack <c_chunk> chunks = new Stack <c_chunk>();

                bool line_corrupt = false;

                foreach (char input_char in input_line)
                {
                    if (line_corrupt)
                    {
                        if (pretty)
                        {
                            Console.ForegroundColor = ConsoleColor.DarkGray;
                            Console.Write(input_char);
                        }

                        continue;
                    }

                    if (input_char == '(' ||
                        input_char == '[' ||
                        input_char == '{' ||
                        input_char == '<')
                    {
                        chunks.Push(new c_chunk(input_char));

                        Console.ForegroundColor = ConsoleColor.Gray;
                        Console.Write(input_char);
                    }
                    else
                    {
                        c_chunk chunk = chunks.Pop();

                        if (input_char != chunk.end_char)
                        {
                            if (pretty)
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.Write(input_char);
                            }
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.WriteLine(
                                    " Corrupt line. Expected {0}, but found {1} instead.",
                                    chunk.end_char,
                                    input_char);
                            }

                            line_corrupt = true;
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.Write(input_char);
                        }
                    }
                }

                if (!line_corrupt)
                {
                    if (chunks.Count != 0)
                    {
                        UInt64 score      = 0;
                        string completion = "";

                        while (chunks.Count != 0)
                        {
                            c_chunk chunk = chunks.Pop();

                            completion += chunk.end_char;

                            score *= 5;
                            score += get_score2(chunk.end_char);
                        }

                        if (pretty)
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write(completion);
                        }
                        else
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.WriteLine(
                                " Incomplete line. Complete by adding {0}. ({1} points)",
                                completion,
                                score);
                        }

                        scores.Add(score);
                    }
                    else
                    {
                        if (!pretty)
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                            Console.WriteLine(" Line valid.");
                        }
                    }
                }

                if (pretty)
                {
                    Console.WriteLine();
                }
            }

            UInt64 result = scores.OrderBy(x => x).ElementAt(scores.Count / 2);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine();
            Console.WriteLine("Result = {0}", result);
            Console.ResetColor();
        }