aoc::year2020

Module 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.