Categories
code perl Tech

Advent of Code 2020 days 8 & 9

Day 8

For part 1 we’re given a list of instructions, one per line, with 3 possible actions taken. Either we alter a value and move on to the next instruction, we move to a different instruction, or skip and move on to the next line. Not too bad, I can set up a $skip variable, such that when jumping forward the instructions aren’t read and then simply print out the value of the accessor once processing the filehandle is finished. Except the jump instruction also takes negative values => trying to readlines from before sounds like a headache for future Juan. Today I just want a solution, not necessarily an efficient one. The first idea is to get this into a hash of hashes such that as each line is read, an index key is paired with another hash containing 3 key-pair values: {'action':$act, ' 'val': $value, 'exec':0}

Now once the data is nicely in the hash of hashes, it needs to be processed. The first thing is to establish the break point to avoid the infinite loop.

if ($instructions{$index}{exec} == 1){
    print ($acc, "\n");
    die; #exit the loop
} 

Next up was running actually doing the tests which meant testing all keys in order was out of the question. The first index seemes to always be 1, so doing it with a while loop means propper control can be implemented:

$max = keys %instructions;
$id = 1;
while ($id < $max){
    # infinite loop control from before
    #
    # check if the actions are jump or acc 
    # and do something
    # else $id++
}

Most likely it was an error with execution, but this was dying 1 instruction too early. Refactored everything so now data was in an array of arrays.

Solution for Part 1

Part 1 worked fine with no issues. Giving the answer incredibly fast. Part 2 on the other hand, required testing possible mutations of the base instructions. Clearly this means I need to test the array once the instructions are processed. Convert the solution for part 1 into a subroutine as above, and then add a test to verify if no single instruction was used more than once.

testing the arrays

From there it was just a matter of controlling the mutations. Or so I thought.

Part 2

Need to ensure an immutable copy of the instructions is kept [X]
Check if the given instruction causes problems [X]
Test the new array [X]
Find the result [ ]
Debugging shows the control works most of the time. However at a certain point, instead of returning $acc the code seems to enter an infinite loop. Something to check in the future.

Day 9

Todays challenge involved testing a data stream for errors. Each line contains a number and following an introduction buffer, each new number has to be the sum of 2 numbers from the previous buffer. My initial thoughts were to hard code the buffer, but that was misreading the question. Once the buffer for the checks is in place, each new item gets added to the list, and the oldest gets discarded. Fairly simple, can be done while processing the input data: check that the first 25 get read in, then start processing.

Processing for part 1 consisted of checking in a nested loop for each item. To avoid doing 252 checks, the moment a solution was found (multiple solutions possible) it would break the loop. Following the data processing, if no solution had been found, it would print the number and die as I no longer needed to process additional data.

For part 2, it was important to find a continuous list of numbers within the entire datastream such that together they added to the flag in part 1. I didn’t want to change the code from part 1 as it may come in handy later on in the advent, so new array @nums2 holds every item from the list.

Now I want to check every possible set of continuous numbers, without leaving the machine running until the 25th. The question implies there are 2 numbers, which means the largest starting index is 2 less than the size of @nums2. (-1 as array counting starts at 0, -1 as the last array holds $nums[-2] and $nums[-1]). That’s the initial for statement sorted. There are 2 critical breaking points:

  • The starting number in the list is greater than the key from part 1
  • The sum is greater than the key found in part 1.

Solved part one by using the next if, and the second with the while ($sum < $part1) control. For some reason I created the @inc array (for included numbers) and forgot about it while trying to finish part 2. Attempted splicing the @nums2 array but somehow kept getting the wrong highest number. Most likely an indexing issue with the splice function. Used the @inc array and suddenly it worked.

As I look at this now, I realise the foreach (@inc) could have been replaced with

sort @inc;
my ($min, $max) = ($inc[0], $inc[-1]);
# or accessed those values directly

Hindsight is, as always, 20/20.

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.