Advent Of Code

Chapter 01: Onboard the Christmas Train

Preamble

It was a pretty unpeculiar day, except perhaps for the unbearable cold outside. I just got home, lit up a candle hoping to warm up my hands with is meager flame.

“Why is it so cold outside?”, I said to myself.

Then it hit me. The reason the temperature was so low:

“Ohh, it’s december 1st already… That explains everything.”, I thought.

But then, something occured to me:

“Wait a minute… DECEMBER 1ST? OH GOSH I NEED TO HURRY UP!”, I exclaimed, rushing to where I needed to be.


I feel like some explanations are due here.

First of all, who am I?

My name is Sheinxy, I’m a Haskell-loving cat, and for the past 6 years I have been saving Christmas.

Next question, where am I going?

Well, to the North Pole obviously! Where else would I go to save Christmas!


Arriving at my destination, it feels immediately obvious that my presence is indeed needed. Some bad weather (I knew it was exceptionnally cold today!) is keeping the elves from flying their planes, so they are (and by that I mean “I am”) going to have to ride the train to gather supplies to make all the presents.

Good, I love trains, and a two weeks expedition through the North Pole sounds actually somewhat relaxing. What could possibly go wrong?

Part one

Before getting onto the train, the Elves need to load up a few items. And, obviously, they need my help for that (how would they survive without me…)

The problem is quite simple, they have a bunch of containers containing something, each container is label with the ID of what they contain, and they have a list of each container’s ID number, which looks like that:

57
78
43
76

They want to find the IDs of the three most common items. Nothing I can’t solve with Haskell!

Parsing

Parsing this list is quite simple: it’s just reading each line as an integer:

type Input = [Int]

parseInput :: String -> Input
parseInput = map read . lines

Finding the three most common items

Finding the three most common items in the list is simple, we just need to count. And you know what, making an histogram seems like a common thing to do, so I’m going to add this function inside my Utils library!

I’m doing it in a very simple, non-optimized way that uses standard function:

The idea is simple: I sort the list so that items of the same value are next to each other, then I group these items together, and finally I transform each group into a pair containing the value and the number of elements in the group.

-- Takes a list of ordered items and returns its histogram
count :: (Ord a) => [a] -> [(a, Int)]
count = map (\xs -> (head xs, length xs)) . group . sort

Now, all I need to do is take that histogram, sort it by most common values, and take the first three IDs (and multiply them together)!

type Output = Int

partOne :: Input -> Output
partOne = product . map fst . take 3 . sortBy (comparing (Down . snd)) . count

Part two

Now I can go on and help the other elves who needs to do the absolute exact same thing, except they’re looking for the three least common items!

type Output = Int

partTwo :: Input -> Output
partTwo = product . map fst . take 3 . sortBy (comparing snd) . count

Chapter Epilogue

Now that everything is loaded, the train can finally depart!


Oh! I’ve just realized that I wanted to write a few more things before ending today’s chapter!

First of all, this year I am using Gram instead of my usual Vim for coding. This doesn’t change much, it’s just a bit prettier (I use it in light mode btw) and I have a LSP for once (yeah, I’ve been coding all this time without one, crazy I know).

Second of all, I don’t know how many of you readers are new here, how many are familiar with Haskell etc. If at some point reading this, or at some point in your life coding in Haskell, you want to look up what a function is, I highly recommand Hoogle! I’ve been using it forever, and it’s such a helpful ressource when it comes to looking up Haskell functions! (Bonus tip if you’re new and interested in Haskell: search for your function’s hypothetical type signature, it might already exist!)

That’s all for now!