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);
                });
            });
        }
Beispiel #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);
                });
            });
        }
        public void MakeMapImage()
        {
            var sb       = new StringBuilder();
            var dir      = Path.Combine(FileHelper.PatchDirectoryName("problems"), "all", "images");
            var problems = ProblemReader.ReadAll();

            foreach (var problemMeta in problems)
            {
                var map = problemMeta.Problem.ToState().Map;
                var bmp = new Image <Rgba32>(Configuration.Default, map.SizeX + 2, map.SizeY + 2, Rgba32.Black);
                foreach (var cell in map.EnumerateCells())
                {
                    bmp[cell.Item1.X + 1, cell.Item1.Y + 1] = GetColor(cell.Item2);
                }

                foreach (var booster in problemMeta.Problem.Boosters)
                {
                    bmp[booster.Position.X + 1, booster.Position.Y + 1] = GetColor(booster.Type);
                }

                var factor = 1;
                if (map.SizeX < 100)
                {
                    factor *= 2;
                }
                if (map.SizeX < 20)
                {
                    factor *= 2;
                }

                bmp.Mutate(x => x.Resize(map.SizeX * factor, map.SizeY * factor, new BoxResampler()));
                bmp.Save(Path.Combine(dir, problemMeta.ProblemId + ".png"));
                sb.Append($"<img style=\"margin:10px\" src=\"{problemMeta.ProblemId}.png\" alt=\"{problemMeta.ProblemId} title=\"{problemMeta.ProblemId}\"\">");
            }
            File.WriteAllText(Path.Combine(dir, "index.html"), sb.ToString());
        }