Skip to content

Commit

Permalink
v2.2.8 - minor changes
Browse files Browse the repository at this point in the history
introduced Response class to `SubmitAnswerAsync()` return
updated `SubmitAnswerAsync()` return type on README
added `.mono` to gitignore
  • Loading branch information
antoniosubasic committed Apr 7, 2024
1 parent a3cb691 commit 3c22bfc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.mono/
.vscode/

## Ignore Visual Studio temporary files, build results, and
Expand Down
25 changes: 19 additions & 6 deletions APIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public async Task<Dictionary<int, int>> GetAllStarsAsync()
/// <param name="part">The part of the puzzle.</param>
/// <param name="answer">The answer to submit.</param>
/// <returns>Returns whether the answer was true or false and a cooldown if existent.</returns>
public async Task<string> SubmitAnswerAsync(int part, object answer)
public async Task<Response> SubmitAnswerAsync(int part, object answer)
{
var content = new StringContent($"level={part}&answer={answer}")
{
Expand All @@ -149,30 +149,30 @@ public async Task<string> SubmitAnswerAsync(int part, object answer)

var response = await SendRequestAsync(HttpMethod.Post, $"https://adventofcode.com/{_year}/day/{_day}/answer", content);

if (response.Contains("That's the right answer!")) { return "True"; }
if (response.Contains("That's the right answer!")) { return true; }
else if (response.Contains("You don't seem to be solving the right level. Did you already complete it?"))
{
var dayResponse = await SendRequestAsync(HttpMethod.Get, $"https://adventofcode.com/{_year}/day/{_day}");
var matches = PuzzleAnswerRegex().Matches(dayResponse);

if (matches.Count >= part) { return (matches[part - 1].Groups["answer"].Value == answer.ToString()).ToString(); }
if (matches.Count >= part) { return matches[part - 1].Groups["answer"].Value == answer.ToString(); }
else { throw new Exception("answer could not be found"); }
}
else if (response.Contains("You gave an answer too recently"))
{
var match = TimeForAnswerTooRecentRegex().Match(response);

if (match.Success) { return $"cooldown left: {match.Groups["time"].Value}"; }
if (match.Success) { return match.Groups["time"].Value; }
else { throw new Exception("time could not be found"); }
}
else if (response.Contains("That's not the right answer.") && response.Contains("before trying again."))
{
var match = TimeForWrongAnswerRegex().Match(response);

if (match.Success) { return $"False\non cooldown: {match.Groups["time"].Value}"; }
if (match.Success) { return (false, match.Groups["time"].Value); }
else { throw new Exception("time could not be found"); }
}
else { return "False"; }
else { return false; }
}

[GeneratedRegex(@"<pre><code>(?<sample>(.*?\n)*?)<\/code><\/pre>")]
Expand All @@ -187,3 +187,16 @@ public async Task<string> SubmitAnswerAsync(int part, object answer)
[GeneratedRegex(@"wait (?<time>.*?) before trying again")]
private static partial Regex TimeForWrongAnswerRegex();
}

public class Response
{
public bool? Value { get; private set; }
public string? Cooldown { get; private set; }

public static implicit operator Response(bool value) => new() { Value = value };
public static implicit operator Response(string cooldown) => new() { Cooldown = cooldown };
public static implicit operator Response((bool value, string cooldown) t) => new() { Value = t.value, Cooldown = t.cooldown };

public override string ToString()
=> $"{(Value is not null ? Value : string.Empty)}{(Value is not null && Cooldown is not null ? "\n" : string.Empty)}{(Cooldown is not null ? $"on cooldown: {Cooldown}" : string.Empty)}";
}
2 changes: 1 addition & 1 deletion AoC.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<PackageTags>AoC, Advent-of-Code, API</PackageTags>
<RepositoryUrl>https://github.com/antonio-subasic/AoC.API</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageVersion>2.2.7</PackageVersion>
<PackageVersion>2.2.8</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Dictionary<int, int> achievedStars = await client.GetAllStarsAsync(); // Retriev
## Submit answer

```csharp
string status = await client.SubmitAnswerAsync(int part, object answer); // Submits the answer for a specific part of the Advent of Code puzzle. Returns whether the answer was true or false and a cooldown if existent.
Response response = await client.SubmitAnswerAsync(int part, object answer); // Submits the answer for a specific part of the Advent of Code puzzle. Returns whether the answer was true or false and a cooldown if existent.
```

<br><br>
Expand Down

0 comments on commit 3c22bfc

Please sign in to comment.