Advent of Code Day 1 to 5

As you have probably noted by the measurable reduction of programmer productivity worldwide, Advent of Code is running again. Here's a short recap of my experiences from day 1 to day 5.

I usually approach the problems straight-forward (or brute-force), mostly because I'm not that kind of smart to know all the nice shortcuts and algorithms (which I later usually read up on the very recommended "Solution Megathreads" on reddit. You can find me solutions here.

Day 1 - Sonar Sweep

Starting of easy, just adding / comparing some values. I still learned something during the second task: When comparing a sliding window of data (at least in this simple case), you only need to compare the first and the last value, because the middle values are affecting both window-values the same way and thus can be ignored:

24    24
53    53

Yes, you can sum up 12+24+53 and compare that with the sum of 24+53+13, but you can just as well ignore 24+53 and just compare 12 and 13.

Day 2 - Dive!

Still simple, a little bit of parsing and the rest was again just adding up numbers.

Day3 - Binary Diagnostic

I had some troubles remembering how to convert a binary sting (1001101) to an int. I first used the rather ugly my $bits='110101'; my $int = eval '0b'.$bits, and only "remembered" about oct() after reading it in some other solutions. But I used unicode var names $γ, $ε :-)

Day 4 - Giant Squid aka Bingo

I again forgot something I already learned in a previous Advent: If the input contains blocks separated by blank lines, it's much easier to just split the input on two newlines instead of remembering state and starting new blocks if there is an empty line...

I convert the horizontal rows and vertical cols into lines, so each bingo card has 10 lines. Each line is a Hash where key and value are the number (i.e. 42 => 42) because I thought that in part 2 we might need to do something with the checked values (with turned out to not be the case, ah, well..)

When a number is drawn, go through all the boards and all the rows, mark the drawn number with a X, and check if a line has 5 Xs (my @checked = grep {/X/} values $line->%*;). If it has, we have a winner, so calc the value and report it.

In theory part 2 only needed to remove the exit, but as I had to remove bingo cards that have already one I had to convert my loop to use an iterator.

Day 5 - Hydrothermal Venture

I had a solution for part 1 quite quickly, but it didn't work for the proper data. So I let the code rest for a bit, went for a run, cooked lunch, and later checked a few of the posts on reddit. And indeed I found the needed tip: I was using a plain sort, which (in Perl) sorts alphanumerically, and not by int value. This was quickly fixed by the spaceship operator: sort { $a <=> $b }.

For the diagonals in part 2 I wasted a lot of time on just mucking with the coordinates, without success. I knew what I had to do, but was too lazy to properly implement it. So late in the afternoon I took pen & paper, drawed some diagrams, and renamed my vars from $x, $y to $row, $col, because I constantly messed up which was which. Also figuring out which direction was which took me an embarrassingly long time...

Next week..

I plan to keep playing (and posting here and on reddit), but let's see what the combination of more complex tasks, work and family will allow :-)