Categories
code perl Tech

Advent of Code 2020 days 6 & 7

Day 6

This day’s challenge required doing some text analysis. Ok, I’d done this in the past as a part of language analysis to measure changes in language over several hundred years. The first part required finding how many questions people answered yes to, from a list of 26 questions… 26 questions, 26 letters. Yep, hardcoded it:
@alphabet = ("a".."z");
Then analysed the text by splitting on empty lines and processing responses with some simple regex:

Regular expressions had to be involved

Using the hash table has the advantage that even if several people answer the same questions, the letter is only used once as a key in the hash table. So, once a group’s answers have been processed and the text finds an empty line, that data needs to be processed:

Initial processing

Once all data is read in, a simple print($answers, "\n"); gives the answer. Part 2 then requires finding those questions where everyone answered yes. For this I’m using an additional value, $groupSize which is incremented at each answer sheet and reset to 0 after processing. This way, upon processing the results, it is easy to compare how many elements in the response data have $groupSize as their value.

The data processing

While the script was rather short while readable, this task was far from trivial. The past experience was an incredible boost in establishing the best data structure to go with.

Day 7

Reading the requirements immediately reminded me of some applied maths concepts. Specifically, unordered graphs. Fairly simple idea, create a node such that the node has a name and either it has contents or it has no contents (end node). Given each row of data was providing the information, it was fairly simple to create the nodes. Creating the graph interface on the other hand, was a whole other nightmare and importing the module was also a further rabbit hole not worth the limited time available for the challenge. Ok, so simply making it a hash of hashes. I can take advantage of regex and controls to make hash tables of varying sizes.

Using regex to process the data for the hash tables

I was able to verify the %bags hash contained the data in the expected and wanted format by using Data::Dumper. Unfortunately after a couple hours it felt as though I’d forgotten how recursion worked, as the ouput I was seeing in terminal suggested an infitnite loop. Definitely a topic I’ll be going back to later on, when there is time.

Categories
code perl Tech

Advent of Code 2020

Days 3, 4 & 5

For day 3 it was required to find how many trees a specific slope would encounter. The field was given as a field of limited width, far smaller than the height. The problem states that the pattern repeats to the right, which means for any real position X, the relative position within the input would be given by modulo division. That is to say if the input has a width of 10, any real position X can be mapped onto the limited width input by X mod 10. For part 2 it was required to find the product of encountered trees across various slopes, which I hard coded and then got the product from.

Day 4

Day 4 had data in key:value pairs but the formatting was not constant accross the entries. The idea was then to split each record into a hash table and test that hash table. For part 1 it was a case of hard coding the required fields and then checking if they existed in the keys of the record (hash table). With return 0 in the case of a negative it reduces the number of tests.

Part 2 required the same tests and then data validation. The three year data fields were simple comparisons. However, the other fields di allow for some interesting regular expressions. By making the check subroutine return 0 upon the first failed test reduced (a little) the amount of tests performed.

Day 5

The input for this challenge consisted in a set of letters representing a seat. The first letters used a binary representation of the row, and the final 3 represented the which seat in that row the seat was. The data is already binary numbers, just with different characters… Regex to the rescue. Capturing the part which was required (row or column), converting to binary and then to decimal => easy to understand. With the getRow and getCol subroutines sorted and tested, all that remained was to find the highest sanity check. Part 1 sorted.

Part 2, perhaps as I was running on remnants of sleep coffee fumes encountered far more issues. Initially I wanted to create an array of anonymous arrays such that if a seat existed it would get marked as “X”, and I could then look up which seat was not labeled as “X”. Except that was not what the questions was asking -> I needed the missing ID. What data structure lets me find items incredibly fast? The hash table. By using the same code from part 1, it was then just a matter of adding each sanity check into a hash table. $seats{$sanity} = 1.

From part 1, the highest sanity check is 801, so simply checking for all numbers up to 801 if exists $seats{$_} and printing all those where the check failed. The terminal printed the numbers 0-39 and then one single ID in the hundreds. The problem stated that the first couple IDs weren’t occupied as they were not seats => verifying that the ids 1 above and 1 below the result existed and voila. Could it have been done more neatly? Yes, but then, that would mean keeping up with perl idioms and using it more than once a year.