Ejemplo n.º 1
0
        private static void TestPart(BaseChallenge challenge, ChallengePart part)
        {
            Console.SetOut(new StringWriter()); // Discard all output during part execution
            Results results = Execute(challenge, part, fullStackTrace: false);

            ConsoleUtil.RestoreDefaultOutput();

            Console.ForegroundColor = (part == ChallengePart.Part1 ? ConsoleColor.Blue : ConsoleColor.DarkCyan);
            Console.Write($"{challenge.day:00}-{(int)part} ");

            results.SetStatusColor();
            switch (results.status)
            {
            case ResultStatus.Development:
            case ResultStatus.Candidate:
                Console.Write("WIP ");
                break;

            case ResultStatus.WrongAnswer:
            case ResultStatus.Exception:
                Console.Write("FAIL");
                break;

            case ResultStatus.Success:
                Console.Write("PASS");
                break;
            }
            Console.ResetColor();
            Console.Write(" ");

            WriteBenchmark();

            Console.ResetColor();
            Console.WriteLine(results.status == ResultStatus.Exception ? results.message : results.givenAnswer);
        }
Ejemplo n.º 2
0
        public static void Test(Type type)
        {
            BaseChallenge challenge = CreateChallengeInstance(type);

            TestPart(challenge, ChallengePart.Part1);
            TestPart(challenge, ChallengePart.Part2);
        }
Ejemplo n.º 3
0
        private static void RunPart(BaseChallenge challenge, ChallengePart part)
        {
            Results results = Execute(challenge, part);

            results.SetStatusColor();
            Console.Write($"[Part {(int)part}]");
            Console.ResetColor();
            Console.Write(" ");

            WriteBenchmark();

            Console.ResetColor();
            string[] messageParts = (results.message ?? string.Empty).Split("{0}");
            if (messageParts.Length > 0)
            {
                Console.Write(messageParts[0]);
            }
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write(results.givenAnswer);
            Console.ResetColor();
            if (messageParts.Length > 1)
            {
                Console.Write(messageParts[1]);
            }
            Console.WriteLine();
        }
Ejemplo n.º 4
0
        public static void Run(Type type)
        {
            BaseChallenge challenge = CreateChallengeInstance(type);

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine($" <<< Advent of Code {challenge.year} Day {challenge.day} >>> ");
            Console.ResetColor();

            RunPart(challenge, ChallengePart.Part1);
            RunPart(challenge, ChallengePart.Part2);

            Console.WriteLine();
        }
Ejemplo n.º 5
0
        private static Results Execute(BaseChallenge challenge, ChallengePart part, bool fullStackTrace = true)
        {
            Results data = new Results();

            try
            {
                Stopwatch.Restart();
                ResetMethod.Invoke(challenge, null);
                object output = SolvePartMethods[part].Invoke(challenge, null);
                Stopwatch.Stop();

                (string message, object answer) = ((string, object)?)output ?? (null, null);

                data.message     = message;
                data.givenAnswer = answer?.ToString();

                string expected = ExpectedAnswerProps[part].GetValue(challenge)?.ToString();
                if (!string.IsNullOrEmpty(expected))
                {
                    data.status = (data.givenAnswer == $"{expected}" ? ResultStatus.Success : ResultStatus.WrongAnswer);
                }
                else if (!string.IsNullOrEmpty(data.givenAnswer))
                {
                    data.status = ResultStatus.Candidate;
                }
                else
                {
                    data.status = ResultStatus.Development;
                    Stopwatch.Reset();
                }
            } catch (Exception ex)
            {
                data.status = ResultStatus.Exception;
                Stopwatch.Reset();

                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;                               // Skip Invoke() and nested exceptions
                }
                data.message = ex.Message;
                if (fullStackTrace)
                {
                    data.message += "\n" + FormatStackTrace(ex.StackTrace);
                }
            }

            return(data);
        }
Ejemplo n.º 6
0
 public static string GetPath(this BaseChallenge challenge) => GetPath(challenge.year, challenge.day);