Exemplo n.º 1
0
 public static string DayName(this Solver solver)
 {
     return($"Day {solver.Day()}");
 }
Exemplo n.º 2
0
        public async Task Upload(Solver solver)
        {
            var color = Console.ForegroundColor;

            Console.WriteLine();
            var solverResult = Runner.RunSolver(solver);

            Console.WriteLine();

            if (solverResult.errors.Any())
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Uhh-ohh the solution doesn't pass the tests...");
                Console.ForegroundColor = color;
                Console.WriteLine();
                return;
            }

            var problem = await DownloadProblem(GetContext(), GetBaseAddress(), solver.Year(), solver.Day());

            if (problem.Answers.Length == 2)
            {
                Console.WriteLine("Both parts of this puzzle are complete!");
                Console.WriteLine();
            }
            else if (solverResult.answers.Length <= problem.Answers.Length)
            {
                Console.WriteLine($"You need to work on part {problem.Answers.Length + 1}");
                Console.WriteLine();
            }
            else
            {
                var level  = problem.Answers.Length + 1;
                var answer = solverResult.answers[problem.Answers.Length];
                Console.WriteLine($"Uploading answer ({answer}) for part {level}...");

                // https://adventofcode.com/{year}/day/{day}/answer
                // level={part}&answer={answer}

                var cookieContainer = new CookieContainer();
                using var handler = new HttpClientHandler()
                      {
                          CookieContainer = cookieContainer
                      };
                using var client = new HttpClient(handler)
                      {
                          BaseAddress = GetBaseAddress()
                      };

                var content = new FormUrlEncodedContent(new[] {
                    new KeyValuePair <string, string>("level", level.ToString()),
                    new KeyValuePair <string, string>("answer", answer),
                });

                cookieContainer.Add(GetBaseAddress(), new Cookie("session", GetSession()));
                var result = await client.PostAsync($"/{solver.Year()}/day/{solver.Day()}/answer", content);

                result.EnsureSuccessStatusCode();
                var responseString = await result.Content.ReadAsStringAsync();

                var config   = Configuration.Default;
                var context  = BrowsingContext.New(config);
                var document = await context.OpenAsync(req => req.Content(responseString));

                var article = document.Body.QuerySelector("body > main > article").TextContent;
                article = Regex.Replace(article, @"\[Continue to Part Two.*", "", RegexOptions.Singleline);
                article = Regex.Replace(article, @"You have completed Day.*", "", RegexOptions.Singleline);
                article = Regex.Replace(article, @"\(You guessed.*", "", RegexOptions.Singleline);
                article = Regex.Replace(article, @"  ", "\n", RegexOptions.Singleline);

                if (article.StartsWith("That's the right answer"))
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(article);
                    Console.ForegroundColor = color;
                    Console.WriteLine();
                    await Update(solver.Year(), solver.Day());
                }
                else if (article.StartsWith("That's not the right answer"))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(article);
                    Console.ForegroundColor = color;
                    Console.WriteLine();
                }
                else if (article.StartsWith("You gave an answer too recently"))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine(article);
                    Console.ForegroundColor = color;
                    Console.WriteLine();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine(article);
                    Console.ForegroundColor = color;
                }
            }
        }
Exemplo n.º 3
0
 public static string WorkingDir(this Solver solver)
 {
     return(WorkingDir(solver.Year(), solver.Day()));
 }