Advent of Code day 18
Today's puzzle Operation Order is a simple arithmetic problem.
The homework (your puzzle input) consists of a series of expressions that consist of addition (+), multiplication (*), and parentheses ((...)). Just like normal math, parentheses indicate that the expression inside must be evaluated before it can be used by the surrounding expression. Addition still finds the sum of the numbers on both sides of the operator, and multiplication still finds the product.
Just kidding -- there's a twist, as usual.
However, the rules of operator precedence have changed. Rather than evaluating multiplication before addition, the operators have the same precedence, and are evaluated left-to-right regardless of the order in which they appear.
In part 2 this changes to:
Now, addition and multiplication have different precedence levels, but they're not the ones you're familiar with. Instead, addition is evaluated before multiplication.
I actually found dealing with the parenthesis to be the more difficult part of this problem. I mean, granted, without the different precedence rules I could just use Python's eval function and be done with the problem in seconds. But the logic for evaluating the expression left to right is not hard.
To pick out the parenthesis, my routine steps through the expression and looks for the first ')', then goes back to the previous '(' -- this accounts for nested parens. With this I can first evaluate the equations inside the parentheses, then go back and do the rest left to right.
For part 2 it was fairly easy to have the code search for additions and do those first, then finish up with multiplications. That's not to say that I didn't run into some strange results and have to debug for awhile, but I didn't have to rethink my original plan for the code, stare at the screen for awhile, take out the scratch paper, etc. Just some basic debugging.
So, 2 more stars down, but I'm a bit behind on puzzles. Lots to do. Onward!