A small .NET template to back you up
Tags: adventofcode, dotnet, csharp
Published: 2022-12-03 14:25:11

As usual, for December, the [Advent of Code](https://adventofcode.com/) challenge has once again started this year.

I've been ~~trying to do~~ doing it for quite some time now, always in C#, and always have had to type the same code:

1. The parser for the input file
1. The solution for the first puzzle
1. The test for the example
1. The test for the solution
1. The solution for the second puzzle
1. The test for the example
1. The test for the solution

It may not be much, but it results in either an inconsistent way of solving the puzzles or in a lot of redundancy in the code for each day.

To spare some time (or at least spare some of yours), I created a small GitHub template repository so that you can quickly get started on the implementation on this year's puzzles and not on the boilerplate to do so:

{% embed https://github.com/pbouillon/dotnet-advent-of-code-template %}

> It also comes with the [GitHub Action's CI](https://github.com/pBouillon/dotnet-advent-of-code-template/blob/main/.github/workflows/dotnet.yml) preconfigured and a [demo project](https://github.com/pBouillon/dotnet-advent-of-code-template/tree/main/src/AdventOfCode.Usage) if you need some guidance.

## Usage

It's usage is fairly simple:

- For your logic, inherit from `Solver`
- To test your logic against the example and find your solution, inherit from `TestEngine`

### The `Solver`

The solver is the place where you can specify where your input is, how and to what you want it to be parsed, and the logic to solve the first and second part of the puzzle.

Let's see its usage with an example:

If the first part of the puzzle is "Given a list of integers, find the greatest one" and the second one "Now find the sum of them", we can do the following:

```csharp
public class Solver : Solver<int[], int>
{
    protected override string InputPath => "input.txt";

    public override int PartOne(int[] input)
        => input.Max();

    public override int PartTwo(int[] input)
        => input.Sum();

    public override int[] ReadInput(string inputPath)
        => File
            .ReadAllLines(inputPath)
            .Select(int.Parse)
            .ToArray();
}
```

### The `TestEngine`

The test engine is the class that will allow you to check if your solutions are right.

For each part, you will be asked to specify what your example is and what solution you are expecting.

> Of course, the solution is unknown at first and making this test fail will show you your solution.

For our example, we may have written the following:

```csharp
public class SolverTest : TestEngine<Solver, int[], int>
{
    public override Puzzle PartOne => new()
    {
        Example = new()
        {
            Input = new[] { 1, 2, 3 },
            Result = 3,
        },

        Solution = 5,
    };

    public override Puzzle PartTwo => new()
    {
        Example = new()
        {
            Input = new[] { 1, 2, 3 },
            Result = 6,
        },

        Solution = 15,
    };
}
```

That's pretty much all about it, I hope that it will help others to get through this year's puzzle, and see you on the 24th! 🎄

---

Photo by <a href="https://unsplash.com/@michelstock?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Michel Stockman</a> on <a href="https://unsplash.com/s/photos/sapin?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>