aoc/year2020/day18.rs
1//! # Operation Order
2//!
3//! For part one the operator precedence is the same so we proceed from left to right for
4//! each expression. Parentheses are handled via recursion, so we return either when encountering
5//! the end of the string or a `)` character.
6//!
7//! For part two whenever we encounter the lower priority `*` operator then we *implicitly* insert
8//! parentheses around the remaining expression. For example:
9//!
10//! * 1 * 2 * 3 * 4 => 1 * (2 * (3 * (4)))
11//! * 1 + 2 * 3 + 4 => 1 + 2 * (3 + 4)
12//! * 1 + (2 * 3 * 4) + 5 => 1 + (2 * (3 * (4))) + 5
13use crate::util::parse::*;
14use std::str::Bytes;
15
16pub fn parse(input: &str) -> Vec<&str> {
17 input.lines().collect()
18}
19
20pub fn part1(input: &[&str]) -> u64 {
21 fn helper(bytes: &mut Bytes<'_>) -> u64 {
22 let mut total = value(bytes, helper);
23
24 while let Some(operation) = next(bytes) {
25 let value = value(bytes, helper);
26 if operation == b'+' {
27 total += value;
28 } else {
29 total *= value;
30 }
31 }
32
33 total
34 }
35
36 input.iter().map(|line| helper(&mut line.bytes())).sum()
37}
38
39pub fn part2(input: &[&str]) -> u64 {
40 fn helper(bytes: &mut Bytes<'_>) -> u64 {
41 let mut total = value(bytes, helper);
42
43 while let Some(operation) = next(bytes) {
44 if operation == b'+' {
45 total += value(bytes, helper);
46 } else {
47 // Implicitly insert '(' and ')' around the remaining sub-expression so when it
48 // finishes we break too.
49 total *= helper(bytes);
50 break;
51 }
52 }
53
54 total
55 }
56
57 input.iter().map(|line| helper(&mut line.bytes())).sum()
58}
59
60/// Convenience wrapper around [`Bytes`] iterator. Encountering a `)` is also considered end of
61/// sequence. The expressions are consistently formatted so encountering a space just means
62/// skip and return the next character that will always be present.
63fn next(bytes: &mut Bytes<'_>) -> Option<u8> {
64 match bytes.next() {
65 None | Some(b')') => None,
66 Some(b' ') => bytes.next(),
67 other => other,
68 }
69}
70
71/// Convenience wrapper to return the value of either the next raw digit literal or a
72/// sub-expression nested in parentheses.
73fn value(bytes: &mut Bytes<'_>, helper: impl Fn(&mut Bytes<'_>) -> u64) -> u64 {
74 match next(bytes).unwrap() {
75 b'(' => helper(bytes),
76 b => b.to_decimal() as u64,
77 }
78}