--- Day 1: Historian Hysteria ---

December 1

Listening to: Lo-fi


This year I'm doing things a little different. I'm doing it in C#, a language I'm already very familiar with, instead of using it as an opportunity to learn. There's a few reasons for this, but the main one is that this year, I wanted to focus solely on solving the problem at hand with the tools I already know, instead of learning something new. And after a few years, I don't think there's much more I can learn about C++ (at least, through the context of Advent of Code).

I'm also taking this year to think things through a bit more this year rather than just jumping into problems. I'm taking the time to build out a bit of a framework to automate things a bit for me, such as fetching the input from the website, running the code a bit more smartly, and time-permitting some quality of life stuff that can perhaps time my code, suggest that it's been running for too long, a library for some common problems I run into, etc.

I started with a basic interface for my problems to run through

namespace AOC
{
    public interface IProblem
    {
        public void Parse(string[] input);

        public int PartOne();
        public int PartTwo();
    }
}

Nothing fancy, but this does mean I can build out a class with that interface, and have a main program that simply makes that day's class, automatically fetch and parse the input, then run the problems and spits out the solutions. At least, that's the idea in the long run. I need to actually build out that infrastructure first; right now this interface is the only real functional piece there is. I'll expand on most of the rest of it this week when I have some more time (as I started on this past 11pm due to some social obligations).


A missing historian... hm...

Part one is absurdly easy. Just difference of pairs, take the absolute value.

int sum = 0;

for (int i = 0; i < left.Count; i++)
{
    sum += Math.Abs(left[i] - right[i]);
}

Part two is simple enough too; find the frequency of values in the first list within the second list. The naieve solution would be to parse through the list for every item (n^2), but just making a frequency dict of the second list would just mean parsing through each list once (2n).

var freqDict = right.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

int sum = 0;
foreach (int val in left)
{
    if (freqDict.TryGetValue(val, out int freq))
    {
        sum += val * freq;
    }
}

Oh man do I miss being able to use LINQ.


Time taken: 10 minutes

My solutions for today's puzzles