aoc-2020-zig

Advent of Code 2020 Solutions in Zig
git clone https://git.sinitax.com/sinitax/aoc-2020-zig
Log | Files | Refs | README | sfeed.txt

part1 (2130B)


      1--- Day 18: Operation Order ---
      2
      3As you look out the window and notice a heavily-forested continent slowly appear over the horizon,
      4you are interrupted by the child sitting next to you. They're curious if you could help them with
      5their math homework.
      6
      7Unfortunately, it seems like this "math" follows different rules than you remember.
      8
      9The homework (your puzzle input) consists of a series of expressions that consist of addition (+),
     10multiplication (*), and parentheses ((...)). Just like normal math, parentheses indicate that the
     11expression inside must be evaluated before it can be used by the surrounding expression. Addition
     12still finds the sum of the numbers on both sides of the operator, and multiplication still finds the
     13product.
     14
     15However, the rules of operator precedence have changed. Rather than evaluating
     16multiplication before addition, the operators have the same precedence, and are
     17evaluated left-to-right regardless of the order in which they appear.
     18
     19For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are as follows:
     20
     211 + 2 * 3 + 4 * 5 + 6
     22  3   * 3 + 4 * 5 + 6
     23      9   + 4 * 5 + 6
     24         13   * 5 + 6
     25             65   + 6
     26                 71
     27
     28Parentheses can override this order; for example, here is what happens if parentheses are added to
     29form 1 + (2 * 3) + (4 * (5 + 6)):
     30
     311 + (2 * 3) + (4 * (5 + 6))
     321 +    6    + (4 * (5 + 6))
     33     7      + (4 * (5 + 6))
     34     7      + (4 *   11   )
     35     7      +     44
     36            51
     37
     38Here are a few more examples:
     39
     40
     41 - 2 * 3 + (4 * 5) becomes 26.
     42 - 5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 437.
     43 - 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 12240.
     44 - ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 13632.
     45
     46
     47Before you can help with the homework, you need to understand it yourself. Evaluate the
     48expression on each line of the homework; what is the sum of the resulting values?
     49
     50