Advent of Code 2017

The Advent of Code (www.adventofcode.com) is an online advent calendar where you will get two programming puzzle each day from December 1st to 25th. This was the third year for it and about 40000 attended to it (solved the 1st puzzle) and nearly 4000 did all 49 puzzles (there was only one puzzle on the 25th day but two on other days).

This was the first time I tried it and I solved 45/49 puzzle. I had heard about it earlier but didn’t have enough time then. It took 1-4 hours per day to solve puzzles so it definitely wasn’t peace of cake. Many of them were not easy. But on the other hand, you will learn a lot programming when you try to solve those.

Form of The Puzzles

At first glance, puzzles are quite regular programming exercises: “create a list that does this and that..”. But the reality is that they aren’t that easy.

First, there is (usually quite long) explanation of the puzzle with a simple example. Puzzles are quite complex so it has to be explained carefully. Every puzzle will have input data and then the program will give an output for it. You can already start to program here and that is what I also usually did.

Then there is always “your input” which will be given as an input to the program. This input is every time really long. You really can’t solve puzzles with pen and paper. Even if the example in explanation could be easily solved with pen and paper, it is practically impossible with “your input”.

And finally, there is a text field where you put your program’s output and submit it. If you are lucky you have correct output.

Example

Day 2 puzzle was one of the simplest one:

The spreadsheet consists of rows of apparently-random numbers. To make sure the recovery process is on the right track, they need you to calculate the spreadsheet’s checksum. For each row, determine the difference between the largest value and the smallest value; the checksum is the sum of all of these differences.

For example, given the following spreadsheet:

5 1 9 5
7 5 3
2 4 6 8
  • The first row’s largest and smallest values are 9 and 1, and their difference is 8.
  • The second row’s largest and smallest values are 7 and 3, and their difference is 4.
  • The third row’s difference is 6.

In this example, the spreadsheet’s checksum would be 8 + 4 + 6 = 18.

What is the checksum for the spreadsheet in your puzzle input?

As you can see this isn’t that difficult puzzle and could be solved with pen and paper. Here is my input for the puzzle:

MyInput

If you think this is too easy take a look at day 3 or day 21 puzzles. I couldn’t solve day 21 and it took few days for me to solve day 3.

My Solution in C#

Here is my solution to day 2 in C#:

public static class CorruptionChecksum
{
    public static int CalculateChecksum(string inputData)
    {
        return inputData.Split('\n').Sum(CalculateMinMaxDifference);
    }
    
    public static int CalculateMinMaxDifference(string row)
    {
        int min;
        int max;
        GetMinMax(row, out min, out max);
    
        return max - min;
    }

    private static void GetMinMax(string row, out int min, out int max)
    {
        min = int.MaxValue;
        max = int.MinValue;
        
        foreach (var numberStr in row.Split('\t'))
        {
            int number = int.Parse(numberStr);
            if (number > max)
                max = number;
            if (number < min)
                min = number;
        }
    }
}

And here is my test code for it:

[Test]
public void CalculateMyChecksumTest()
{
    Assert.AreEqual(
        *****,
        CorruptionChecksum.CalculateChecksum(Resources.MyInput));
}

[Test]
public void CalculateChecksumTest()
{
    Assert.AreEqual(
        18,
        CorruptionChecksum.CalculateChecksum(Resources.ExampleInput));
}

[TestCase("5\t1\t9\t5", 8)]
[TestCase("7\t5\t3", 4)]
[TestCase("2\t4\t6\t8", 6)]
public void CalculateMaxMinDifferenceTest(string row, int expected)
{
    var actual = CorruptionChecksum.CalculateMinMaxDifference(row);
    Assert.AreEqual(expected, actual);
}

One of the best parts, in my opinion, is the main function that returns the puzzle output:

return inputData.Split('\n').Sum(CalculateMinMaxDifference);

It is surprisingly quite a functional way to solve it even if I am an object-oriented programmer. But I have to admit that ReSharper helped me a bit with that.

Summary

I really recommend the Advent of Code for those who already can program and are learning a new programming language. But for beginners, it would be too difficult. And of course, if you like to challenge yourself like I did, go on! I will definitely try to solve past years’ puzzles when I am learning my next programming language (which I am trying to do every year).

You will (re)learn a lot when you try to solve those puzzles. I learned a lot about C# libraries and recursion for example and programming generally also. Here are some things I learned or learned more about:

  • implementing trees,
  • how to travel in trees,
  • Tuple (first time I really used it),
  • HashSet,
  • Dictionary (a lot!),
  • different loops (a lot!),
  • recursion (a lot!),
  • handling lists, arrays etc. (a lot!),
  • hexa/ascii conversions,
  • LINQ (a lot!),
  • etc.

I think Advent of Code would have been easier with functional programming. When I looked for some solutions and conversations on Reddit, functional solutions looked surprisingly easy. Object-oriented thinking, which I prefer, maybe wasn’t best. And if I would’ve been better with LINQ that would also help me. But I learned to use LINQ lot better than I used to.

Ps. I started to read “Functional Programming in C#” by Enrico Buonano after Advent of Code to learn functional programming. So far it seems to be a good book. Probably I will write a review of it after I’ve read it.

One thought on “Advent of Code 2017

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s