public static void Register(CommandLineApplication app)
        {
            app.Command(
                "solve",
                (command) =>
            {
                command.Description = "Solve all problems with all solvers";
                command.HelpOption("-?|-h|--help");

                var solverOption = command.Option(
                    "-s|--solver",
                    "Solver name prefix",
                    CommandOptionType.SingleValue);

                var problemsOption = command.Option(
                    "-p|--problems",
                    "Single problem id or problem ids range",
                    CommandOptionType.SingleValue);

                command.OnExecute(
                    () =>
                {
                    var solvers = RunnableSolvers
                                  .Enumerate()
                                  .Select(x => x.Invoke())
                                  .Where(x => !solverOption.HasValue() || solverOption.HasValue() && x.GetName().StartsWith(solverOption.Value()))
                                  .ToList();

                    var problemIds = new List <int>();
                    if (problemsOption.HasValue())
                    {
                        if (int.TryParse(problemsOption.Value(), out var problemId))
                        {
                            problemIds.Add(problemId);
                        }
                        else
                        {
                            var parts  = problemsOption.Value().Split(new [] { ".." }, StringSplitOptions.RemoveEmptyEntries);
                            var pStart = int.Parse(parts[0]);
                            var pEnd   = int.Parse(parts[1]);
                            problemIds.AddRange(Enumerable.Range(pStart, pEnd - pStart + 1));
                            Console.WriteLine($"Will solve problems: {string.Join(", ", problemIds)}");
                        }
                    }

                    solvers.ForEach(
                        solver =>
                    {
                        ProblemReader
                        .ReadAll()
                        .Where(x => !problemIds.Any() || problemIds.Contains(x.ProblemId))
                        .ToList()
                        .ForEach(problemMeta => Common.Solve(solver, problemMeta));
                    });

                    return(0);
                });
            });
        }
Пример #2
0
        public static void Register(CommandLineApplication app)
        {
            app.Command(
                "solve-unsolved",
                (command) =>
            {
                command.Description = "Create solutions for all nonexistent problem-solver pairs";
                command.HelpOption("-?|-h|--help");

                var threadsOption = command.Option(
                    "-t|--threads",
                    "Number of worker threads",
                    CommandOptionType.SingleValue);

                command.OnExecute(
                    () =>
                {
                    var threadsCount = threadsOption.HasValue() ? int.Parse(threadsOption.Value()) : Environment.ProcessorCount;
                    var threads      = Enumerable.Range(0, threadsCount).ToList();
                    Parallel.ForEach(
                        threads,
                        new ParallelOptions {
                        MaxDegreeOfParallelism = threadsCount
                    },
                        thread =>
                    {
                        while (true)
                        {
                            var solvers = RunnableSolvers
                                          .Enumerate()
                                          .OrderBy(_ => Guid.NewGuid())
                                          .Select(x => x.Invoke())
                                          .ToList();

                            var problems = ProblemReader.ReadAll();

                            solvers.ForEach(
                                solver =>
                            {
                                var solved   = Storage.EnumerateSolved(solver).Select(x => x.ProblemId);
                                var unsolved = problems
                                               .Select(x => x.ProblemId)
                                               .Except(solved)
                                               .OrderBy(_ => Guid.NewGuid())
                                               .ToList()
                                               .First();

                                Common.Solve(solver, problems.Find(x => x.ProblemId == unsolved), thread);
                            });
                        }
                    });

                    return(0);
                });
            });
        }