Basically what I did is to solve the innermost expressions in the parenthesis and use this partial result to solve the outer expressions recursively. In case of part two, it is a bit trickier, since there is operator precedence. In this case when I encountered a multiplication, I've split the expression in 2 parts, basically solving the additions first and applying the multiplications afterwards for the partial results.
Here is my code in Java. It's a bit hefty, because Java, but it does the job:
Today's problem is the perfect candidate for using a recursive divide-and-conquer approach
Basically what I did is to solve the innermost expressions in the parenthesis and use this partial result to solve the outer expressions recursively. In case of part two, it is a bit trickier, since there is operator precedence. In this case when I encountered a multiplication, I've split the expression in 2 parts, basically solving the additions first and applying the multiplications afterwards for the partial results.
Here is my code in Java. It's a bit hefty, because Java, but it does the job:
All my solutions can be found at: github.com/Ernyoke/advent-of-code-...