Module aoc::year2020::day18

source ·
Expand description

§Operation Order

For part one the operator precedence is the same so we proceed from left to right for each expression. Parentheses are handled via recursion, so we return either when encountering the end of the string or a ) character.

For part two whenever we encounter the lower priority * operator then we implicitly insert parentheses around the remaining expression. For example:

  • 1 * 2 * 3 * 4 => 1 * (2 * (3 * (4)))
  • 1 + 2 * 3 + 4 => 1 + 2 * (3 + 4)
  • 1 + (2 * 3 * 4) + 5 => 1 + (2 * (3 * (4))) + 5

Functions§

  • next 🔒
    Convenience wrapper around Bytes iterator. Encountering a ) is also considered end of sequence. The expressions are consistently formatted so encountering a space just means skip and return the next character that will always be present.
  • value 🔒
    Convenience wrapper to return the value of either the next raw digit literal or a sub-expression nested in parentheses.