Skip to content

Commit 4c83674

Browse files
committed
Create a strategy to handle the remote inputs
1 parent a50a98e commit 4c83674

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Net;
2+
3+
namespace AdventOfCode.Commons.Input;
4+
5+
public class RemotePuzzleInputReaderStrategy : IPuzzleInputReaderStrategy
6+
{
7+
private const string AdventOfCodeBaseUrl = "https://adventofcode.com";
8+
9+
public const string Cookie = "53616c7465645f5f24418ae85f2ee16ffe226239823eded5e30b5f5dee7817447b92dd8ac6207ed1838b8ef68429c61bac187a1e2b7e7cd1e747ea82fe417a96";
10+
11+
public required int Year { get; init; }
12+
13+
public required int Day { get; init; }
14+
15+
private static HttpClientHandler Handler => new()
16+
{
17+
CookieContainer = GetCookieContainer(),
18+
UseCookies = true,
19+
};
20+
21+
private static HttpClient Client => new(Handler)
22+
{
23+
BaseAddress = new Uri(AdventOfCodeBaseUrl),
24+
};
25+
26+
private static CookieContainer GetCookieContainer()
27+
{
28+
var container = new CookieContainer();
29+
30+
container.Add(new Cookie
31+
{
32+
Name = "session",
33+
Domain = ".adventofcode.com",
34+
Value = string.IsNullOrEmpty(Cookie)
35+
? throw new Exception("You need to specify your cookie in order to get your input puzzle")
36+
: Cookie,
37+
});
38+
39+
return container;
40+
}
41+
42+
public IEnumerable<string> ReadInput()
43+
{
44+
var task = Task.Run(async () =>
45+
{
46+
var response = await Client.GetAsync($"/{Year}/day/{Day}/input");
47+
48+
return await response.EnsureSuccessStatusCode()
49+
.Content
50+
.ReadAsStringAsync();
51+
});
52+
53+
task.Wait();
54+
55+
var content = task.Result;
56+
return content.Split("\n")[..^1];
57+
}
58+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using AdventOfCode.Commons;
2+
3+
namespace AdventOfCode.Usage.WithRemoteInput;
4+
5+
public class Solver : Solver<int[], int>
6+
{
7+
public Solver() : base(2021, 01) { }
8+
9+
public override int[] ParseInput(IEnumerable<string> input)
10+
=> input.Select(int.Parse).ToArray();
11+
12+
public override int PartOne(int[] input)
13+
{
14+
var count = 0;
15+
16+
for (var i = 1; i < input.Length; ++i)
17+
{
18+
if (input[i - 1] < input[i]) ++count;
19+
}
20+
21+
return count;
22+
}
23+
24+
public override int PartTwo(int[] input)
25+
{
26+
var windowSums = Enumerable.Range(0, input.Length - 2)
27+
.Select(offset => input[offset..(offset + 3)].Sum())
28+
.ToArray();
29+
30+
return PartOne(windowSums);
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using AdventOfCode.Commons;
2+
3+
namespace AdventOfCode.Usage.WithRemoteInput;
4+
5+
public class SolverTest : TestEngine<Solver, int[], int>
6+
{
7+
public override Puzzle PartOne => new()
8+
{
9+
Example = new()
10+
{
11+
Input = new[] { 199, 200, 208, 210, 200, 207, 240, 269, 260, 263 },
12+
Result = 7,
13+
},
14+
Solution = 1696,
15+
};
16+
17+
public override Puzzle PartTwo => new()
18+
{
19+
Example = new()
20+
{
21+
Input = new[] { 199, 200, 208, 210, 200, 207, 240, 269, 260, 263 },
22+
Result = 5,
23+
},
24+
Solution = 1737,
25+
};
26+
}

0 commit comments

Comments
 (0)