Day 3. Mull It Over¶
Part one¶
The shopkeeper wants our help with the computer. The program wants to run the multiplication, but computer memory is corrupted. Our goal is to run operations we can correctly identify as multiplication and return the sum of the results.
The only correct variant of the operation is: mul(X,Y)
where X and Y are three-digit numbers.
After all the troubles with the previous day I've decided to use simple regular expressions to extract the operations from the input and calculate the result. While this approach may not be the most efficient, it works well for our input size. For a more robust or scalable solution, writing a dedicated lexer could be an interesting alternative to explore.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
Part two¶
This part is almost the same as the first one. But now we need to account for
do()
and don't()
instructions:
- do()
enables future mul()
operations
- don't()
disables future mul()
operations
We start with the active state. I've decided to use a boolean flag for this and
check it before performing the multiplication if the match is mul()
operation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Tags¶
- regular expressions
- parsing