Only a developer can save christmas
Ow no, it happened again! Something threatens Christmas and we only get to know what it is on the first of December, 0:00. The thing is, only a really good software developer will be able to save Christmas!
Solving Advent of Code 2022, saving Christmas
You probably think I started drinking mulled wine too soon, too much! But the premise above is the storyline of a yearly coding challenge called Advent of Code. If you never heard of it, it`s an advent calendar with two daily challenges.
Each day you're supposed to solve the first challenge after which you get access to the second one. (Spoiler alert: the second challenge often makes you curse your first solution). The challenges vary in skill level and skill sets, though you'll probably find the first few easier than the last few.
Solutions often require you to come up with clever solutions. And if you don't want your solution to run for hours on end, you'll also need to keep performance and complexity in mind. You'll find soon enough that brute-forcing isn't always the best option!
How to be this years hero
To start solving the challenges you'll login at https://adventofcode.com/2022/auth/login to identify yourself. Everyone participating will face the same challenges, but every input and subsequent answer will be personalized.
If you're reading this before 12-01 0:00, you'll have to wait for the first day to unlock. If not, you can go to https://adventofcode.com/2022/day/1. You're first challenge will be waiting for you there.
Advent of code can be solved in any way you see fit. You could try it with pen and paper, but since the inputs often span hundreds, if not thousands, of lines of data I would use software!
It's also up to you what language and framework you use. In the past I've tried rotating several languages I wanted to get better at. This year I'll stick to C# because my goal this year is finishing the challenges, not learning new things π.
The rest of this post will go over how to setup a project for Advent of Code in C#!
Advent of code, the C# way
You don't need much to get started with solving your challenges. You need something that will run and a way to see the output. So a simple Console app will suffice.
Visual Studio
The easiest way to get this up and running is with Visual Studio. Simply create a new project via File β New β Project. Search for Console App
and select the one for C#. Continue through the wizard by giving your project and solution a name, select your framework version (I'll be using 7.0) and finally hit Create
.
Dotnet CLI
If you don't want to use Visual Studio you can get the same result by using the Dotnet CLI:
We start by creating a AdventOfCode2022.sln
file in a new folder called AdventOfCode2022
_10dotnet new sln --output AdventOfCode2022
Next we create the Console project
_10dotnet new console --output AdventOfCode2022/Console
And add it to our solution with the following commands
_10cd AdventOfCode_10dotnet sln add Console
Now you can open this folder in your favorite editor and build the whole solution with dotnet build
or run the Console project with dotnet run --project Console
Finally we need to read our input. The code for this is pretty easy:
_10var input = File.ReadAllLines("input.txt");
This code presumes you have the input file in the root of you project. However, when you try to run this you get a FileNotFoundException
. This is because the input.txt isn't copied when building the project. Dotnet moves all files to \bin\Debug\net7.0
, but input.txt is left behind. To remedy this we have to tell Visual Studio to Always Copy the file in the properties.
Or we can add the following to our Console.csproj (which is what Visual Studio does when setting the property):
_10<ItemGroup>_10 <None Update="input.txt">_10 <CopyToOutputDirectory>Always</CopyToOutputDirectory>_10 </None>_10</ItemGroup>
Practice your Test Driven Development
Advent of code is perfect for practicing Test Driven Development. Each challenge includes one or more examples which you could use for testing your code. The challenges are also quite small, making it easy to start writing tests before you start writing the actual code.
To start with TDD we have to add a Test project to our solution. We can follow pretty much the same steps as above. But instead of adding an Console application, we add a xUnit Test Project
. In Visual Studio we can do this by right-clicking on the solution β add β new project. With the dotnet
command we can use the following commands from the same folder where our solution file lives:
_10dotnet new xunit --output Console.Test_10dotnet sln add Console.Test
Finally, we have to reference our Console project in our Console.Test project. In Visual studio we can do this by right-clicking our Console.Test project β Add β Project Reference and then select our Console project.
For the dotnet CLI people we use:
_10dotnet add Console.Test/Console.Test.csproj reference Console/Console.csproj
Good developers are lazy developers
Saving Christmas is hard work. And though you now know how to setup everything you need. You'd rather have someone else do it for you.
Luckily for you, I have created a template you can use. It includes a more fleshed out version of what I described above.
To use the template we need to use the dotnet cli, so open up your console and type:
_10dotnet new install Larsv94.Templates.AoCStarter::1.0.0_10dotnet new aocstarter -n AdventOfCode2022
Join me in saving Christmas (again)
Advent of Code has become some sort of tradition for me. I've started in 2018 and have done all subsequent years, which are still visible on my personal Github page. However, I haven't finished one yet!
Advent of Code is a perfect way to challenge yourself or your colleagues, learn a new language, practice for an interview or get better in your favorite programming language!
I hope you will join me this year!
What to read next:
I really hope you enjoyed this article. If you did, you might want to check out some of these articles I've written on similar topics.
- Read Three ways to structure .NET Minimal APIsβ 9 minutes readThree ways to structure .NET Minimal APIs
- Read A 10 minute introduction to C# Attributesβ 8 minutes readA 10 minute introduction to C# Attributes
- Read Generate OpenAPI documentation for dynamic query parameters in .NET 7β 7 minutes readGenerate OpenAPI documentation for dynamic query parameters in .NET 7