As you look out the window and notice a heavily-forested continent slowly appear over the horizon, you are interrupted by the child sitting next to you. They're curious if you could help them with their math homework.
Unfortunately, it seems like this "math" follows different rules than you remember.
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.
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.
For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6
are as follows:
1 + 2 * 3 + 4 * 5 + 6
3 * 3 + 4 * 5 + 6
9 + 4 * 5 + 6
13 * 5 + 6
65 + 6
71
Parentheses can override this order; for example, here is what happens if parentheses are added to form 1 + (2 * 3) + (4 * (5 + 6))
:
1 + (2 * 3) + (4 * (5 + 6))
1 + 6 + (4 * (5 + 6))
7 + (4 * (5 + 6))
7 + (4 * 11 )
7 + 44
51
Here are a few more examples:
2 * 3 + (4 * 5)
becomes26
.5 + (8 * 3 + 9 + 3 * 4 * 3)
becomes437
.5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))
becomes12240
.((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2
becomes13632
.
Before you can help with the homework, you need to understand it yourself. Evaluate the expression on each line of the homework; what is the sum of the resulting values?
Your puzzle answer was 4491283311856
.
You manage to answer the child's questions and they finish part 1 of their homework, but get stuck when they reach the next section: advanced math.
Now, addition and multiplication have different precedence levels, but they're not the ones you're familiar with. Instead, addition is evaluated before multiplication.
For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6
are now as follows:
1 + 2 * 3 + 4 * 5 + 6
3 * 3 + 4 * 5 + 6
3 * 7 * 5 + 6
3 * 7 * 11
21 * 11
231
Here are the other examples from above:
1 + (2 * 3) + (4 * (5 + 6))
still becomes51
.2 * 3 + (4 * 5)
becomes46
.5 + (8 * 3 + 9 + 3 * 4 * 3)
becomes1445
.5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))
becomes669060
.((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2
becomes23340
.
What do you get if you add up the results of evaluating the homework problems using these new rules?
Your puzzle answer was 68852578641904
.
This is all about writing a parser and evaluator with (custom) operator precedence rules -- a thing I always hated. For part 1, this is relatively simple, as there are only parentheses to take care of. Part 2, however, requires a full-blown parser with parenthesis support and at least two levels of precedence. There are many algorithms to choose from; I opted for the shunting-yard algorithm, which is simple enough and converts an expression from infix notation into postfix notation (a.k.a. reverse polish notation, RPN) which, in turn, is easy to evaluate.
An alternate implementation is possible though, with a little bit of cheating: By defining a custom class with overloaded operators that swap the meaning of multiplication and addition, and some pre-processing of the input string, the whole thing can be eval
'd by Python itself, leading to a much cleaner, shorter and faster solution.
- Part 1, Python: 263 bytes, <100 ms
- Part 2, Python (manual parsing): 392 bytes, <100 ms
- Part 2, Python (
eval
hack): 236 bytes, <100 ms