challenge.txt 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. --- Day 18: Operation Order ---
  2. 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.
  3. Unfortunately, it seems like this "math" follows different rules than you remember.
  4. 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.
  5. 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.
  6. For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are as follows:
  7. 1 + 2 * 3 + 4 * 5 + 6
  8. 3 * 3 + 4 * 5 + 6
  9. 9 + 4 * 5 + 6
  10. 13 * 5 + 6
  11. 65 + 6
  12. 71
  13. Parentheses can override this order; for example, here is what happens if parentheses are added to form 1 + (2 * 3) + (4 * (5 + 6)):
  14. 1 + (2 * 3) + (4 * (5 + 6))
  15. 1 + 6 + (4 * (5 + 6))
  16. 7 + (4 * (5 + 6))
  17. 7 + (4 * 11 )
  18. 7 + 44
  19. 51
  20. Here are a few more examples:
  21. 2 * 3 + (4 * 5) becomes 26.
  22. 5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 437.
  23. 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 12240.
  24. ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 13632.
  25. 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?
  26. Your puzzle answer was 18213007238947.
  27. --- Part Two ---
  28. 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.
  29. Now, addition and multiplication have different precedence levels, but they're not the ones you're familiar with. Instead, addition is evaluated before multiplication.
  30. For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are now as follows:
  31. 1 + 2 * 3 + 4 * 5 + 6
  32. 3 * 3 + 4 * 5 + 6
  33. 3 * 7 * 5 + 6
  34. 3 * 7 * 11
  35. 21 * 11
  36. 231
  37. Here are the other examples from above:
  38. 1 + (2 * 3) + (4 * (5 + 6)) still becomes 51.
  39. 2 * 3 + (4 * 5) becomes 46.
  40. 5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 1445.
  41. 5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 669060.
  42. ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 23340.
  43. What do you get if you add up the results of evaluating the homework problems using these new rules?
  44. Your puzzle answer was 388966573054664.