Today’s location to search for the chief historian is familiar (Again! Makes sense, you’ve been to many interesting places.).
You remember visiting it.
The elves in charge of the place ask you to help with their computer.
Something is wrong with the computer’s memory.
Ah, this feels like christmas! Going to places you haven’t been in a while and people asking you to fix their computer.
Next thing you know, you’ll be reinstalling the elves’ printer drivers.
That’s your input, the memory.
An example input looks like this:
The goal of the program is multiplying some numbers.
It does that with instructions like mul(X,Y), where X and Y are each 1-3 digit numbers.
It then multiplies X by Y.
Because the memory has been corrupted, there are many invalid characters that should be ignored.
Only valid mul(X,Y) instructions should be considered.
Part 1
If you know me, you might know I have a strong irrational hatred of regular expressions.
But even I have to admit, they’ll do well today.
HOWEVER
I coded 2 solutions again, one using regex, and a (much faster, mind you!) custom parsing thing-a-majig (solution, the word I was looking for was solution).
The question asks for the sum of all valid multiplication instructions.
Option 1: regex
Option 2: custom logic
Part 2
There are 2 additional instructions in the corrupted memory to pay attention to.
do() enables mul instructions
don't() disables mul instructions
The most recent one of these instructions applies.
If you come across a mul, but the previous conditional was a don't(), ignore that mul.
In other words, those instructions flip a boolean from true to false,
and you should only do mul if that boolean is true.
mul instructions start as enabled. (the boolean starts as true)