Template project for solving Advent of Code in C#, running on .NET 6.0.
- Simple configuration with
config.json
. - Fetches puzzle input from adventofcode.com and stores it locally.
- Includes various useful utilities for typical puzzle problems.
If you haven't already, use the button shown below (or this link) to create a new repository of your own from this template.
Be sure to create a config.json
before running the project as it won't be able to fetch your puzzle input if you down.
Feel free to make any modifications you want. However, you probably do not want to remove any files outside of AdventOfCode/Solutions/
unless you know what you're doing.
If any solution files that you need are not already included, see Generating Previous Year's Solution Files.
Create config.json
with the following key/value pairs. If you run the program without adding a config.json
file, one will be created for you without a cookie field. The program will not be able to fetch puzzle inputs from the web before a valid cookie is added to the configuration.
{
"cookie": "session=c0nt3nt",
"year": 2020,
"days": [0]
}
cookie
- Note that c0nt3nt
must be replaced with a valid cookie value that your browser stores when logging in at adventofcode.com. Instructions on locating your session cookie can be found here: wimglenn/advent-of-code-wim#1
year
- Specifies which year you wish to output solutions for when running the project. Defaults to the current year if left unspecified.
days
- Specifies which days you wish to output solutions for when running the project. Defaults to current day if left unspecified and an event is actively running, otherwise defaults to 0
.
The field supports list comprehension syntax and strings, meaning the following notations are valid.
"1..4, 10"
- runs day 1, 2, 3, 4, and 10.[1, 3, "5..9", 15]
- runs day 1, 3, 5, 6, 7, 8, 9, and 15.0
- runs all days
Write your code solutions to advent of code within the appropriate day classes in the Solutions folder, and run the project. From the command line you may do as follows.
> cd AdventOfCode
> dotnet build
> dotnet run
Using dotnet run
from the root of the repository will also work as long as you specify which project to run by adding -p AdventOfCode
. Note that your config.json
must be stored in the location from where you run your project.
To specify a debug input instead of your official puzzle input, create a file with the name DayXX-debugInput
side-by-side with your solution and set the UseDebugInput
to true.
You can also directly set the DebugInput value in the constuctor of the solution class if you don't want to use the file.
Use the included PowerShell script AdventOfCode/UserScripts/GenerateSolutionFiles.ps1
to generate a year's solution files following the same layout as those already included.
Usage: GenerateSolutionFiles.ps1 [-Year <Int>]
If no value is provided it will generate files for the current year. The script will avoid overwriting existing files.
Requires PowerShell v3 or later due to the way $PSScriptRoot
behaves. If you have Windows 8+ you should be set. Upgrades for previous versions, and installs for macOS and Linux can be found in Microsoft's Powershell Documentation
- Code may be written in the solution constructor if it will be beneficial to both parts of the problem (such as parsing the data). Example:
[DayInfo(7, 2022, "")]
class Day07 : ASolution
public Day07() : base() //add true to the call to the base constructor to use debug input
{
string[] lines = Input.SplitByNewLine();
foreach (string line in Lines)
{
//Parse out input here
}
}
protected override string SolvePartOne()
{
//Manipulations specific to Part 1 here
return result;
}
protected override string SolvePartTwo()
{
//Manipulations specific to Part 2 here
return result;
}
}
- The variable
Input
will contain your input as a long raw string. - If stuck you can set the
DebugInput
variable at the top of the constructor, and it will overwriteInput
variable, so you won't need to change all your references. - The extension method
SplitByNewLine()
will do exactly that, example:string[] lines = Input.SplitByNewLine()
will split your input into lines for enumeration.
When running your Solutions with a Debugger attached e.g. VSCode or Visual Studio the ASolution base class will try to pause/break the debugger when there is an uncaught Exception thrown by your solution part. This allows for inspection with the debugger without having to specifically set-up additional exception handling within the debugger or your solution.
Sure! Check out the contributing guide.