Skip to content

Commit 6834459

Browse files
committed
Add the missing documentation
1 parent 6c1ffcc commit 6834459

File tree

7 files changed

+94
-35
lines changed

7 files changed

+94
-35
lines changed

src/AdventOfCode.Commons/Input/IPuzzleInput.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/AdventOfCode.Commons/Input/LocalPuzzleInputReaderStrategy.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace AdventOfCode.Commons.PuzzleInputReaderStrategy;
2+
3+
/// <summary>
4+
/// Defines the strategy to be used to read the puzzle input
5+
/// </summary>
6+
public interface IPuzzleInputReaderStrategy
7+
{
8+
/// <summary>
9+
/// Read the lines composing puzzle input
10+
/// </summary>
11+
///
12+
/// <returns>
13+
/// The puzzle input as an enumeration of its lines
14+
/// </returns>
15+
///
16+
/// <remarks>
17+
/// The trailing blank line is omitted
18+
/// </remarks>
19+
IEnumerable<string> ReadInput();
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace AdventOfCode.Commons.PuzzleInputReaderStrategy;
2+
3+
/// <summary>
4+
/// Strategy to retrieve the input from a local file at a given path
5+
/// </summary>
6+
public class LocalPuzzleInputReaderStrategy : IPuzzleInputReaderStrategy
7+
{
8+
/// <summary>
9+
/// The path where the puzzle input is located
10+
/// </summary>
11+
public required string InputPath { get; init; }
12+
13+
/// <inheritdoc />
14+
public IEnumerable<string> ReadInput()
15+
=> File.ReadAllLines(InputPath);
16+
}

src/AdventOfCode.Commons/Input/RemotePuzzleInputReaderStrategy.cs renamed to src/AdventOfCode.Commons/PuzzleInputReaderStrategy/RemotePuzzleInputReaderStrategy.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,53 @@
11
using System.Net;
22

3-
namespace AdventOfCode.Commons.Input;
3+
namespace AdventOfCode.Commons.PuzzleInputReaderStrategy;
44

5+
/// <summary>
6+
/// Strategy to retrieve the input from the advent of code server
7+
/// </summary>
58
public class RemotePuzzleInputReaderStrategy : IPuzzleInputReaderStrategy
69
{
710
private const string AdventOfCodeBaseUrl = "https://adventofcode.com";
8-
9-
public const string Cookie = "53616c7465645f5f24418ae85f2ee16ffe226239823eded5e30b5f5dee7817447b92dd8ac6207ed1838b8ef68429c61bac187a1e2b7e7cd1e747ea82fe417a96";
1011

12+
/// <summary>
13+
/// The year targeted by this reader
14+
/// </summary>
1115
public required int Year { get; init; }
12-
16+
17+
/// <summary>
18+
/// The day of the puzzle whose entry we want to retrieve
19+
/// </summary>
1320
public required int Day { get; init; }
1421

22+
/// <summary>
23+
/// The inner HTTP client handler, preconfigured to use cookies and provide the value set in
24+
/// <see cref="Configuration.CookieValue"/>
25+
/// </summary>
1526
private static HttpClientHandler Handler => new()
1627
{
1728
CookieContainer = GetCookieContainer(),
1829
UseCookies = true,
1930
};
2031

32+
/// <summary>
33+
/// The inner HTTP client, preconfigured to make requests to the advent of code URI
34+
/// </summary>
2135
private static HttpClient Client => new(Handler)
2236
{
2337
BaseAddress = new Uri(AdventOfCodeBaseUrl),
2438
};
2539

40+
/// <summary>
41+
/// Create a new cookie container with the one set in <see cref="Configuration.CookieValue"/>
42+
/// </summary>
43+
///
44+
/// <returns>
45+
/// The cookie container for the <see cref="HttpClientHandler"/> to use
46+
/// </returns>
47+
///
48+
/// <exception cref="Exception">
49+
/// Thrown if it is called but <see cref="Configuration.CookieValue"/> is not set
50+
/// </exception>
2651
private static CookieContainer GetCookieContainer()
2752
{
2853
var container = new CookieContainer();
@@ -31,14 +56,15 @@ private static CookieContainer GetCookieContainer()
3156
{
3257
Name = "session",
3358
Domain = ".adventofcode.com",
34-
Value = string.IsNullOrEmpty(Cookie)
59+
Value = string.IsNullOrEmpty(Configuration.CookieValue)
3560
? throw new Exception("You need to specify your cookie in order to get your input puzzle")
36-
: Cookie,
61+
: Configuration.CookieValue,
3762
});
3863

3964
return container;
4065
}
4166

67+
/// <inheritdoc />
4268
public IEnumerable<string> ReadInput()
4369
{
4470
var task = Task.Run(async () =>

src/AdventOfCode.Commons/Solver.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using AdventOfCode.Commons.Input;
1+
using AdventOfCode.Commons.PuzzleInputReaderStrategy;
22

33
namespace AdventOfCode.Commons;
44

@@ -15,20 +15,33 @@ namespace AdventOfCode.Commons;
1515
/// </typeparam>
1616
public abstract class Solver<TInput, TResult>
1717
{
18-
public readonly Lazy<TInput> Input;
18+
/// <summary>
19+
/// The puzzle input
20+
/// </summary>
21+
public readonly TInput PuzzleInput;
1922

23+
/// <summary>
24+
/// Create a new solver and initialize its puzzle input based on the provided <see cref="IPuzzleInputReaderStrategy"/>
25+
/// </summary>
26+
/// <param name="puzzleInputReader">The strategy to use to retriever the puzzle input</param>
2027
private Solver(IPuzzleInputReaderStrategy puzzleInputReader)
2128
{
22-
Input = new Lazy<TInput>(() =>
23-
{
24-
var content = puzzleInputReader.ReadInput();
25-
return ParseInput(content);
26-
});
29+
var content = puzzleInputReader.ReadInput();
30+
PuzzleInput = ParseInput(content);
2731
}
2832

33+
/// <summary>
34+
/// Create a new solver with a puzzle input located in a local file
35+
/// </summary>
36+
/// <param name="inputPath">The path to the file containing the input</param>
2937
protected Solver(string inputPath)
3038
: this(new LocalPuzzleInputReaderStrategy { InputPath = inputPath }) { }
3139

40+
/// <summary>
41+
/// Create a new solver that will retrieve the puzzle input from the server
42+
/// </summary>
43+
/// <param name="year">The year of the current challenge</param>
44+
/// <param name="day">The day for which the puzzle input is needed</param>
3245
protected Solver(int year, int day)
3346
: this(new RemotePuzzleInputReaderStrategy { Year = year, Day = day }) { }
3447

src/AdventOfCode.Commons/TestEngine.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public record Example
3232
{
3333
/// <summary>
3434
/// The data of the input mentionned in the example, in the same form as the
35-
/// <see cref="Solver{TInput, TResult}.Input"/>
35+
/// <see cref="Solver{TInput, TResult}.PuzzleInput"/>
3636
/// </summary>
3737
public TInput Input { get; init; } = default!;
3838

@@ -71,8 +71,7 @@ public record Puzzle
7171
/// </summary>
7272
private readonly TSolver _solver;
7373

74-
protected TestEngine()
75-
=> _solver = new TSolver();
74+
protected TestEngine() => _solver = new TSolver();
7675

7776
#region Part #1
7877

@@ -99,10 +98,10 @@ public void PartOneSolutionTest()
9998
Skip.If(PartOne.ShouldSkipTests, "Puzzle.ShouldSkipTests has been set to true, test skipped");
10099

101100
// Arrange
102-
var input = _solver.Input;
101+
var input = _solver.PuzzleInput;
103102

104103
// Act
105-
var result = _solver.PartOne(input.Value);
104+
var result = _solver.PartOne(input);
106105

107106
// Assert
108107
result.Should().Be(PartOne.Solution);
@@ -135,10 +134,10 @@ public void PartTwoSolutionTest()
135134
Skip.If(PartTwo.ShouldSkipTests, "Puzzle.ShouldSkipTests has been set to true, test skipped");
136135

137136
// Arrange
138-
var input = _solver.Input;
137+
var input = _solver.PuzzleInput;
139138

140139
// Act
141-
var result = _solver.PartTwo(input.Value);
140+
var result = _solver.PartTwo(input);
142141

143142
// Assert
144143
result.Should().Be(PartTwo.Solution);

0 commit comments

Comments
 (0)