Operation Order


Fork me on GitHub
2020-12-18

Day 18: Operation Order

Description:
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.

Unfortunately, it seems like this "math" follows different rules than you remember.

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.

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.

For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are as follows:

1 + 2 * 3 + 4 * 5 + 6
3 * 3 + 4 * 5 + 6
9 + 4 * 5 + 6
13 * 5 + 6
65 + 6
71

Parentheses can override this order; for example, here is what happens if parentheses are added to form 1 + (2 * 3) + (4 * (5 + 6)):

1 + (2 * 3) + (4 * (5 + 6))
1 + 6 + (4 * (5 + 6))
7 + (4 * (5 + 6))
7 + (4 * 11 )
7 + 44
51

Here are a few more examples:

2 * 3 + (4 * 5) becomes 26.
5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 437.
5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 12240.
((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 13632.

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?

--- Part Two ---

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.

Now, addition and multiplication have different precedence levels, but they're not the ones you're familiar with. Instead, addition is evaluated before multiplication.

For example, the steps to evaluate the expression 1 + 2 * 3 + 4 * 5 + 6 are now as follows:

1 + 2 * 3 + 4 * 5 + 6
3 * 3 + 4 * 5 + 6
3 * 7 * 5 + 6
3 * 7 * 11
21 * 11
231

Here are the other examples from above:

1 + (2 * 3) + (4 * (5 + 6)) still becomes 51.
2 * 3 + (4 * 5) becomes 46.
5 + (8 * 3 + 9 + 3 * 4 * 3) becomes 1445.
5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4)) becomes 669060.
((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 23340.

What do you get if you add up the results of evaluating the homework problems using these new rules?

Input:
(3 + 8 * 9 * (4 * 6 + 3 + 4)) * 9 + 9 * 7 * ((9 * 7) + 4)
(9 + (9 * 3 + 2 * 2 + 6 * 2) + 7 + 9) + 5
8 * 7 * (3 + (2 * 5 + 8)) + 6 + 8 + 6
(4 * 6 + 7 + 7 * 4 + 7) * (5 + 6 * 6 * 6 * 8) * 8 * (5 * 4 * 9 + 4 + 3 * 9) * 3
(8 + 8 * 2) * 9 * 5 + (2 + 2 + 3 * 7 * 5 + 6) + 5
(4 * (2 + 4) + 3 + (7 * 5 * 6 * 5 + 2 + 9)) + 8 * ((6 * 6 + 2 + 7 + 2) * (4 * 9 * 7 * 4 * 7)) + 8 * 2 * 4
(5 + 4 + 6) * (6 * 7 + 5 * 9 * 6) * 6 + ((4 + 5 + 3 * 5 + 6) * 6 + 2)
(4 * 7) * 2 * (2 * 9 * 6 + 8)
7 * (3 * 4 * 5 * 8)
(8 * (9 + 5 * 5 + 9 + 5 + 2) * 6) * (4 + 5 * 9 + (7 + 2 + 8) + 5 * 7) + 3
(2 * 4 * (3 * 4 * 5) + 5) * 2 + (6 * 4) + ((4 * 4) * 3 + 2 * 6) + 9 * ((7 * 6 * 8 + 2 + 7 + 9) * 7 + 3 * (8 + 7) * 6)
6 * 3 * (9 * 2 + 8)
2 + (6 * 4) * 7 * 7
2 + (9 + (9 + 7 + 3 * 4 + 6 * 7) * 9) * (7 * (5 * 8 + 8 + 6 + 6) * 7)
(2 + (5 + 5 + 9 * 4) + 3) * 2 + 2 * 6 * 4
5 * 3 * ((8 + 8 + 7 + 3 + 2) + 9)
((9 * 4 + 5 + 7) * 8 * 3 * 6 * (7 + 8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 6 * (9 + 4))
9 + (8 + (7 * 5 * 7 * 5 * 7)) + 2
(4 * 5 * 6 * 3) + 9
6 + (2 * 5 * 9 + 8 + 2) * (2 * 8 * 3 * 2)
3 * (5 * (3 * 5 + 3) + 5 + (2 + 6 * 8 * 2 + 4 + 6) * 2) * 9 * (6 + 6 + 3) * 2
(2 + 5 + 8) + (2 + 8 + 3 * 7 + 4) * 3 + 3 * 9
6 * 6
5 * 9 * ((6 * 5 + 9 + 9) * 6) * 4
5 + 5 * 6 + (3 * 5) + 5
3 + (8 + 8 * (6 + 9 * 4 + 8 * 6) + 6 + 7 * 3) + 2 + 8 * 6 * 7
(3 * 2 + 6 * 3) * 7 * 6 * 9 + 5
2 + ((6 + 4 + 7 + 5 * 3 * 8) * 3 * 5)
(3 * 9 * 5 + 3 + 2) * 4 + 8 + (6 + 3 * 8 * 8) * (7 + 5 + 8) + (8 + 9)
5 * 2 + 2 * 8 * 5 * (2 * 5)

use crate::common::AdventOfCodeDay;

pub struct Day18 {
    input: Vec<String>,
}

impl Day18 {
    pub fn new() -> Self {
        let input_bytes = include_bytes!("../res/18_input.txt");
        let input_str = String::from_utf8_lossy(input_bytes);
        
        let lines = input_str
                        .lines()
                        .map(|p| String::from(p))
                        .collect::<Vec<String>>();

        Self {
            input: lines
        }
    }
}

impl Day18 {
    fn full_eval_lin(formula: String) -> i64 {
        return Self::eval_lin(&formula.chars().filter(|p| *p != ' ').collect(), 0).0;
    }

    fn eval_lin(formula: &Vec<char>, start_idx: usize) -> (i64, usize) {

        let mut curr: i64 = 0;

        let mut i = start_idx;
        loop {
            if i >= formula.len() || formula[i] == ')' {
                return (curr, i+1);
            }

            let mut op = '+';

            if i > start_idx {
                op = formula[i];
                i += 1;
            }

            let param: i64;
            if formula[i] == '(' {
                (param, i) = Self::eval_lin(formula, i+1);
            } else {
                param = formula[i].to_string().parse::<i64>().unwrap();
                i += 1;
            }

            match op {
                '+' => { curr += param; }
                '*' => { curr *= param; }
                _   => panic!()
            }
        }
    }
}

impl Day18 {
    // Shunting-yard Algorithm
    fn eval_advanced(formula: String) -> i64 {
        let mut operands: Vec<i64> = Vec::new();
        let mut operators: Vec<char> = Vec::new();

        for chr in formula.chars().filter(|p| *p != ' ') {
            if chr == '*' || chr == '+' {

                while !operators.is_empty() && chr <= *operators.last().unwrap() {
                    let a = operands.pop().unwrap();
                    let b = operands.pop().unwrap();
                    let op = operators.pop().unwrap();
                    
                    operands.push(match op {
                        '+' => a+b,
                        '*' => a*b,
                        _   => panic!(),
                    });
                }

                operators.push(chr);

            } else if chr == '(' {
                
                operators.push(chr);
                
            } else if chr >= '0' && chr <= '9' {
                
                operands.push(chr.to_string().parse().unwrap());
                
            } else if chr == ')' {

                while *operators.last().unwrap() != '(' {
                    let a = operands.pop().unwrap();
                    let b = operands.pop().unwrap();
                    let op = operators.pop().unwrap();
                    
                    operands.push(match op {
                        '+' => a+b,
                        '*' => a*b,
                        _   => panic!(),
                    });
                }
                operators.pop();

            } else {

                panic!();

            }
        }
        
        while !operators.is_empty() {
            let a = operands.pop().unwrap();
            let b = operands.pop().unwrap();
            let op = operators.pop().unwrap();
            
            operands.push(match op {
                '+' => a+b,
                '*' => a*b,
                _   => panic!(),
            });
        }

        return operands.pop().unwrap();
    }
}

impl AdventOfCodeDay for Day18 {

    fn task_1(&self) -> String {

        if is_verbose!() {
            for line in &self.input {
                verboseln!("{}   :=   {:?}", line, Day18::full_eval_lin(line.to_owned()));
            }
        }

        return self.input.iter().map(|p| Day18::full_eval_lin(p.to_owned())).sum::<i64>().to_string();

    }

    fn task_2(&self) -> String  {

        if is_verbose!() {
            for line in &self.input {
                verboseln!("{}   :=   {:?}", line, Day18::eval_advanced(line.to_owned()));
            }
        }

        return self.input.iter().map(|p| Day18::eval_advanced(p.to_owned())).sum::<i64>().to_string();

    }
}
Result Part 1: 75592527415659
Result Part 2: 360029542265462


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript