Skip to content

Commit

Permalink
updated version 2
Browse files Browse the repository at this point in the history
added sample input fetching
  • Loading branch information
antoniosubasic committed Dec 4, 2023
1 parent af95ac3 commit ce90949
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
12 changes: 12 additions & 0 deletions APIHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ private async Task<string> SendRequestAsync(HttpMethod method, string uri, HttpC
}
}

public async Task<string> GetSampleInputTextAsync(int part = 1)
{
var response = await SendRequestAsync(HttpMethod.Get, $"https://adventofcode.com/{_year}/day/{_day}");

var regex = new Regex(@"<pre><code>(?<sample>(.*?\n)*?)<\/code><\/pre>");
var matches = regex.Matches(response);

if (matches.Count >= part) { return matches[part - 1].Groups["sample"].Value.TrimEnd('\n'); }
else { throw new Exception("sample could not be found"); }
}
public async Task<string[]> GetSampleInputLinesAsync(int part = 1) => (await GetSampleInputTextAsync(part)).Split('\n');

public async Task<string> GetInputTextAsync() => (await SendRequestAsync(HttpMethod.Get, $"https://adventofcode.com/{_year}/day/{_day}/input")).TrimEnd('\n');
public async Task<string[]> GetInputLinesAsync() => (await GetInputTextAsync()).Split('\n');

Expand Down
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.1.5</PackageVersion>
<PackageVersion>2.2.5</PackageVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ a simple [Advent of Code](https://adventofcode.com) API
- [Add NuGet package](#add-nuget-package)
- [Session initialization](#session-initialization)
- [Features](#features)
- [Get input file](#get-input-file)
- [Get input](#get-input)
- [Get sample input](#get-sample-input)
- [Get achieved stars](#get-achieved-stars)
- [Submit answer](#submit-answer)

Expand Down Expand Up @@ -48,11 +49,20 @@ var client = new Session("session cookie", string input, Regex pattern);

# Features

## Get input file
## Get input

```csharp
string inputText = await client.GetInputTextAsync(); // input file (raw text)
string inputLines = await client.GetInputLinesAsync(); // input file (lines array)
string inputText = await client.GetInputTextAsync(); // input (raw text)
string inputLines = await client.GetInputLinesAsync(); // input (lines array)
```

<br>

## Get sample input

```csharp
string sampleInputText = await client.GetSampleInputTextAsync(int part); // sample input (raw text)
string sampleInputLines = await client.GetSampleInputLinesAsync(int part); // sample input (lines array)
```

<br>
Expand Down

0 comments on commit ce90949

Please sign in to comment.