Skip to content

Commit 2f1c12f

Browse files
version 2 bug fix
corrected AoC access-url bug fixed SubmitAnswer + return type changed to string
1 parent 0ded66e commit 2f1c12f

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

APIHandler.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ private async Task<string> SendRequest(HttpMethod method, string uri, HttpConten
4949
}
5050
}
5151

52-
public async Task<string> GetInputText() => (await SendRequest(HttpMethod.Get, $"https://www.adventofcode.com/{_year}/day/{_day}/input")).TrimEnd('\n');
52+
public async Task<string> GetInputText() => (await SendRequest(HttpMethod.Get, $"https://adventofcode.com/{_year}/day/{_day}/input")).TrimEnd('\n');
5353
public async Task<string[]> GetInputLines() => (await GetInputText()).Split('\n');
5454

5555
public async Task<Dictionary<int, int>> GetAllStars()
5656
{
57-
var response = (await SendRequest(HttpMethod.Get, $"https://www.adventofcode.com/events"))
57+
var response = (await SendRequest(HttpMethod.Get, $"https://adventofcode.com/events"))
5858
.Split('\n')
5959
.Where(line => line.StartsWith("<div class=\"eventlist-event\">"))
6060
.ToArray();
@@ -71,22 +71,30 @@ public async Task<Dictionary<int, int>> GetAllStars()
7171
}).ToDictionary(x => x.Year, x => x.Stars);
7272
}
7373

74-
public async Task<bool> SubmitAnswer(int part, object answer)
74+
public async Task<string> SubmitAnswer(int part, object answer)
7575
{
7676
var content = new StringContent($"level={part}&answer={answer}")
7777
{
7878
Headers = { ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded") }
7979
};
8080

81-
var response = await SendRequest(HttpMethod.Post, $"https://www.adventofcode.com/{_year}/day/{_day}/answer", content);
81+
var response = await SendRequest(HttpMethod.Post, $"https://adventofcode.com/{_year}/day/{_day}/answer", content);
8282

83-
if (response.Contains("That's the right answer!")) { return true; }
84-
else if (response.Contains("Your puzzle answer was"))
83+
if (response.Contains("That's the right answer!") || response.Contains("You don't seem to be solving the right level. Did you already complete it?")) { return "True"; }
84+
else if (response.Contains("You gave an answer too recently") || response.Contains("before trying again"))
8585
{
86-
var regex = new Regex(@"<p>Your puzzle answer was <code>(?<answer>.*?)</code>.*?</p>");
87-
var matches = regex.Matches(response);
88-
return matches.Count >= part && answer.ToString() == matches[part - 1].Groups["answer"].Value;
86+
var regex = (
87+
new Regex(@"You have (?<time>.*?) left to wait"),
88+
new Regex(@"wait (?<time>.*?) before trying again")
89+
);
90+
91+
var match = (
92+
regex.Item1.Match(response),
93+
regex.Item2.Match(response)
94+
);
95+
96+
return $"on cooldown: {(match.Item1.Success ? match.Item1 : match.Item2).Groups["time"].Value}";
8997
}
90-
else { return false; }
98+
else { return "False"; }
9199
}
92100
}

AoC.API.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageTags>AoC, Advent-of-Code, API</PackageTags>
1212
<RepositoryUrl>https://github.com/antonio-subasic/AoC.API</RepositoryUrl>
1313
<PackageReadmeFile>README.md</PackageReadmeFile>
14-
<PackageVersion>2.1.3</PackageVersion>
14+
<PackageVersion>2.1.4</PackageVersion>
1515
</PropertyGroup>
1616

1717
<ItemGroup>

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Dictionary<int, int> achievedStars = client.GetAllStars(); // all user's achieve
6868
## Submit answer
6969

7070
```csharp
71-
bool succeeded = client.SubmitAnswer(int part, object answer); // submits answer to initialized year and day, returns true if answer is correct
71+
string status = client.SubmitAnswer(int part, object answer); // submits answer to initialized year and day, returns "True" or "False" or "on cooldown: {cooldown}"
7272
```
7373

7474
<br><br>

0 commit comments

Comments
 (0)