Day 16
Today was a very annoying one! I suspect the BITS algorithm will be used again later this month as a mini throwback to everyone's favourite intcode from 2019; a few things indicate this such as the operators and numbers which all have room to extend, and the fact that the Length Type IDs have 11 or 15 bits assigned to them - thinking about that number in denary is extremely large and hence it will probably be used in the future.... time to save my code :D
After having read Eric's essay(!) I chose a recursive approach. For Part 1, once I understood the problem (which took about 10 minutes), writing a solution was not too difficult . My idea was to continuously chop down the size of the input until it reached nothing, extracting all of the useful information along the way. The few clever bits, such as using zfill(len(x)*4) to account for leading 0s in the input (which I didn't have anyway) were the key bits to the puzzle. Also note that global was used so that I could add to total t within the function. The function has three core parts to it:
If the ID is 4 - we then have to work out the end of this packet to know when the next packet begins and therefore get it's value. The idea here was to go through every 5 characters and if it was a 0 (i.e. if that groups of 5 started with a 0) then we know it has to be the last group therefore we know the end of the packet
If the length type ID was 1 - we needed to extract the number of packets. This means we have to recursively call the function as many times as the number of packets because the function identifies one packet each time
If the length type ID was 0 - we need to change the data and feed that into the function to parse until it is done
Part 2 was a relatively minor edit. I just had to pass on the integer values out of the function as well, and action upon them. Note that to use the prod command, you have to import it from math (which I forgot to include in the solution above).
My brain hurts too much (thanks, recursion); off I go to bed again...