示例#1
0
        private static (string solution, double elapsedTime) SolvePart(bool isPart1, BaseProblem problem)
        {
            Stopwatch stopwatch = new Stopwatch();
            var       solution  = string.Empty;

            try
            {
                Func <string> solve = isPart1
                    ? problem.Solve_1
                    : problem.Solve_2;

                stopwatch.Start();
                solution = solve();
            }
            catch (NotImplementedException)
            {
                solution = "[[Not implemented]]";
            }
            catch (Exception e)
            {
                solution = e.Message + Environment.NewLine + e.StackTrace;
            }
            finally
            {
                stopwatch.Stop();
            }

            var elapsedMilliseconds = CalculateElapsedMilliseconds(stopwatch);

            return(solution, elapsedMilliseconds);
        }
示例#2
0
        private static ElapsedTime SolveProblem(BaseProblem problem, Table table, double constructorElapsedTime, SolverConfiguration configuration)
        {
            var problemIndex = problem.CalculateIndex();
            var problemTitle = problemIndex != default
                ? $"Day {problemIndex}"
                : $"{problem.GetType().Name}";

            if (configuration.ShowConstructorElapsedTime)
            {
                RenderRow(table, problemTitle, $"{problem.GetType().Name}()", "-----------", constructorElapsedTime, configuration);
            }

            (string solution1, double elapsedMillisecondsPart1) = SolvePart(isPart1: true, problem);
            RenderRow(table, problemTitle, "Part 1", solution1, elapsedMillisecondsPart1, configuration);

            (string solution2, double elapsedMillisecondsPart2) = SolvePart(isPart1: false, problem);
            RenderRow(table, problemTitle, "Part 2", solution2, elapsedMillisecondsPart2, configuration);

            if (configuration.ShowTotalElapsedTimePerDay)
            {
                RenderRow(table, problemTitle, "[bold]Total[/]", "-----------", constructorElapsedTime + elapsedMillisecondsPart1 + elapsedMillisecondsPart2, configuration);
            }

            table.AddEmptyRow();

            return(new ElapsedTime(constructorElapsedTime, elapsedMillisecondsPart1, elapsedMillisecondsPart2));
        }