Exemplo n.º 1
0
        static async Task RunOneTimeAsync(CLIOptions options)
        {
            ProblemSolver solver = new ProblemSolver(options.LogProgress);

            using (var apiaryClient = new ApiaryHttpClient())
            {
                Console.WriteLine("Getting problem ...");
                var problemDefinition = await apiaryClient.GetProblemDefinitionAsync();

                Console.WriteLine($"Solving {problemDefinition.Id}");

                Stopwatch sw   = Stopwatch.StartNew();
                string    path = solver.Solve(problemDefinition);
                sw.Stop();
                Console.WriteLine($"Solved [{sw.ElapsedMilliseconds}ms]");
                Console.WriteLine("Posting solution");
                var problemSolution = new ProblemSolutionDto
                {
                    Path = path
                };
                var response = await apiaryClient.PutSolution(problemDefinition, problemSolution);

                Console.WriteLine($"Valid: {(response.Valid ? "yes" : "no")}");
                Console.WriteLine($"In time: {(response.InTime ? "yes" : "no")}");
                if (!string.IsNullOrEmpty(response.Message))
                {
                    Console.WriteLine($"Message: {response.Message}");
                }
            }
        }
Exemplo n.º 2
0
        static async Task RunMineAsync(CLIOptions options)
        {
            var solver = new ProblemSolver(options.LogProgress);

            using (var apiaryClient = new ApiaryHttpClient(options.SaveApiCalls))
                using (var solutionService = options.SaveSolutions ? new ProblemSolutionService() : null)
                {
                    int       fails             = 0;
                    const int MaxFailInRowCount = 3;
                    DateTime  time = DateTime.MinValue;
                    while (true)
                    {
                        time = await ThrottleAsync(time, ApiaryHttpClient.RequiredDelay);

                        var problemDefinition = await apiaryClient.GetProblemDefinitionAsync();

                        Console.WriteLine($"Solving {problemDefinition.Id}");
                        Stopwatch sw   = Stopwatch.StartNew();
                        string    path = solver.Solve(problemDefinition);
                        sw.Stop();
                        Console.WriteLine($"Solved [{sw.ElapsedMilliseconds}ms]");
                        time = await ThrottleAsync(time, ApiaryHttpClient.RequiredDelay);

                        var problemSolution = new ProblemSolutionDto
                        {
                            Path = path
                        };
                        var response = await apiaryClient.PutSolution(problemDefinition, problemSolution);

                        if (!response.Valid || !response.InTime)
                        {
                            var ex = new Exception($"Invalid solution for problem={problemDefinition.Id}");
                            ex.Data[nameof(ProblemSolutionDto)]   = problemSolution;
                            ex.Data[nameof(ProblemDefinitionDto)] = problemDefinition;
                            throw ex;
                        }

                        if (options.SaveSolutions)
                        {
                            var added = await solutionService?.RegisterSolutionAsync(problemDefinition.Id, path);

                            if (!added)
                            {
                                fails++;
                            }
                            else
                            {
                                fails = 0;
                            }

                            if (fails == MaxFailInRowCount)
                            {
                                break;
                            }
                        }
                    }
                }
        }
Exemplo n.º 3
0
        public async Task BasicResultPutTest()
        {
            var response = await apiaryClient.PutSolution(new ProblemDefinitionDto { Id = 2727 }, new ProblemSolutionDto { Path = "DLDRRU" });

            response.Message.Should().NotBe("Data (in HTTP request body) are in wrong format or does not contain resolution");
        }