Hero Image
- Philipp Ludewig

Advent of Code 2021 Day 3

Aloha people,

It's one of these days. Usually they happen on a Monday, but this time it's a shitty Friday. I will keep this post short. Yesterday I described my suspicion that today's puzzle would be much more complex, and from the first look of it I was right. The good old submarine got some cracks, and we need to figure out what is happening. Good for us is that there is a diagnostic system, the problem is that the output is rather cryptic:

00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010

When ever I see a puzzle input like this I immediately think of binary numbers and yes that is today's topic. In part One, the goal is to find the gamma rate and the epsilon rate through checking each column of the numbers and determine the most and least common number. More of the explanation here. Anyway so far so understandable.

My first idea was to create a list of tuples which would contain the count for 0 or 1 for the respective column. My ThoughtWorks advent of code buddies (Grabriel, Predo and Andrei) had an easier idea to solve the puzzle. Just sum the “1” for each column and then check whether the count for each column is larger than the half-length of a column. This means that a one was more prominent than a zero in the column. Through this plan, it's less hassle to check which binary should be used for the given column. We quickly implemented the code in JavaScript and got the puzzle solution for Gabriels puzzle input. Here are the important code bits:

var numberPerColumn = sumResult >= array.length / 2 ? 1 : 0

After implementing the solution in JavaScript, it was time for Rust, which brings me to the WTF moment of the day. I tried to do the same as we did before: populate the array with zero, iterate over the lines for the column and trying to increase the value in the vector. At this point, I was trying something which is impossible in Rust. You cannot change the value of reference. After some more trying, I just gave up. My role model implemented the algorithm in a better-looking way: the code

Me reading his solution:

In a wrongly placed moment of eagerness, I decided to port the solution to Java. Oh boy! Why did I do that? The whole time of learning, Rust showed itself in my coding skills. I had the feeling I was writing my first lines of Java and I wished that the documentation would be as good as the one from Rust. The Java documentation seriously needs to step up and get back in the game. I just love that Rust method always show you an explanation on how it could be used and explain the details. As a beginner, I am loving Rust for that.

Next time I will post sometime next week as I am off on the weekend. I will meet some friends and their offspring. It's incredible to life in a time when you see your university buddies becoming fathers and mothers.