What is Advent of Code and Why You Should Do It

I want to start this post by thanking Wastl, who originally came up with the idea of creating Advent of Code back in 2015. Many have copied it since, and he is the only person who maintains the project.

 

Project website: https://adventofcode.com/

 

 

1 - What is AdventOfCode?

Advent of Code is nothing more than a set of programming challenges held during the month of December in celebration of the Christmas holidays.

It's similar to the traditional advent calendar, but with code.

 

The challenges are quite complicated, well, for juniors, they're quite tough, not because of the code itself, but because each challenge comes with a description based on "real-world" cases (usually problems that elves need to solve to help Santa Claus). This is what makes it tricky, since it's easy to overlook details or miss something important.

Personally, I spend about an hour on each test.

 

Note: the tests are in English

 

 

2 - Why should you do AdventOfCode?

In my opinion, it's worth doing because they are very entertaining challenges. The community is deeply involved, with groups and forums all over the internet discussing solutions and ways to solve them better or differently. But my two main reasons are as follows:

  1. It's a way to dive into another programming language. What many people do is, every year, they tackle the challenges using a different programming language, this year it might be C#, next year JavaScript, the following year PHP, etc. It's a great way to explore and learn the basics of each language.
  2. It can help you gain advanced knowledge about Arrays. While most problems can be solved without advanced knowledge, having these skills helps you write shorter code;
    For example, there are people who manage to solve the exercises in "one instruction", this is done in C# by using LINQ. And believe me, you need a lot of LINQ knowledge to pull it off in a single line.

 

 

3 - Running Advent Of Code in C#

I know it's a bit late in the event, in fact, there are only 4 days left until it ends, but next year I want to do it in C# since this year, what I've done, was in ruby (7 days), and I always had the problem that there wasn't a library to make my life easier. I don't want that for C#.

 

So I've created the library Netmentor.AdventOfcode available on GitHub and on Nuget, which makes life a bit easier.

This library mainly consists of an abstract class called AoCSolution that comes with a series of constructors and two methods.

The methods are:

  • string Part1()
  • string Part2()

Representing the two parts found in every AdventOfCode problem.

And then there are multiple constructors to make it easier to initialize the challenges.

 

The format for the constructors is as follows:

protected AoCSolution(string path, string filename = "input.txt")
protected AoCSolution(int year, int day, string filename = "input.txt")
protected AoCSolution(string path, AoCFileType type = AoCFileType.Input)
protected AoCSolution(int year, int day, AoCFileType type = AoCFileType.Input)

AoCFiletype is basically an enum that accepts `AoCFileType.Input` and AoCFiletype.Test.

 

This is because it's a standard among AdventOfCode users to have a file called input.txt with the data provided by the system, and some people (myself included) use a test file to try out the algorithm with fewer data to make debugging easier.

 

Also, if you choose the year and day option, you'll use the relative path to get the files using 2022/01 (for the year 2022 and day 1), another standard for AdventOfCode users.

Finally, the contents of your file will be loaded into the Input variable.

To use the library, you simply create a class that implements the abstract class:

public class Day01 : AoCSolution
{   
    public Day01(AoCFileType filetype) : base(2022, 01, filetype)
    {
            
    }

    public override string Part1()
    {
        return Input.....;
    }

    public override string Part2()
    {
        throw new NotImplementedException();
    }
}

 

The next step is to either create a test, a console application, etc. In my case, I use tests, and here’s how that would look:

[Fact]
public void ResolveDay1()
{
    Day01 day01 = new Day01(AoCFileType.Input);
    string result1 = day01.Part1();
    Assert.Equal("10130", result1);

    string result2 = day01.Part2();
    Assert.Equal("10005", result2);
}

And this would be an example of the folder structure:

advent of code folder structure

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café