Compare commits

..

3 Commits

Author SHA1 Message Date
25855b47a6 Day 23 part 1 solution 2020-12-23 20:47:52 -08:00
5900b4d3c6 Day 18 part 2 BROKEN. 2020-12-23 19:03:07 -08:00
5b10da61a4 Day 18 part 1 solution and comprehensive tests 2020-12-22 19:57:51 -08:00
4 changed files with 692 additions and 13 deletions

View File

@ -0,0 +1 @@
872495136

View File

@ -31,6 +31,27 @@
//! ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2 becomes 13632. //! ((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? //! 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?
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
@ -65,13 +86,87 @@ fn lex(input: &str) -> Vec<Token> {
.collect() .collect()
} }
fn parse(tokens: &[Token]) -> u64 { fn parse_part1(tokens: &[Token]) -> u64 {
/* /*
let mut p = Parser::default(); let mut p = Parser::default();
dbg!(&p); dbg!(&p);
tokens.into_iter().for_each(|t| p.add_token(t)); tokens.into_iter().for_each(|t| p.add_token(t));
p.eval() p.eval()
*/ */
let mut stack = vec![Vec::new()];
let mut cur_stack = 0;
// Reverse the token list so we can pop them.
let mut tokens: Vec<_> = tokens.into_iter().rev().collect();
while let Some(t) = tokens.pop() {
let mut peek = || {
let t = tokens.pop().unwrap();
tokens.push(t);
t
};
match t {
Token::Num(n) => {
let t2 = peek();
if let Token::Add = t2 {
stack.push(vec![]);
cur_stack += 1;
}
match stack[cur_stack].last() {
Some(Token::Add) => {
stack[cur_stack].pop();
if let Some(Token::Num(left)) = stack[cur_stack].pop() {
stack[cur_stack].push(Token::Num(left + n));
}
}
Some(Token::Mul) => {
stack[cur_stack].pop();
if let Some(Token::Num(left)) = stack[cur_stack].pop() {
stack[cur_stack].push(Token::Num(left * n));
}
}
None => stack[cur_stack].push(*t),
c => {
dbg!(&c);
}
}
}
Token::Add => stack[cur_stack].push(*t),
Token::Mul => stack[cur_stack].push(*t),
Token::Open => {
stack.push(vec![]);
cur_stack += 1;
}
Token::Close => {
// Take the result of this parenthetical group and push it on to the stack one
// level below.
assert_eq!(stack[cur_stack].len(), 1);
let t = stack[cur_stack].pop().unwrap();
stack.pop();
cur_stack -= 1;
stack[cur_stack].push(t);
// If the stack has 3 things, it was waiting for this result.
let len = stack[cur_stack].len();
if len >= 3 {
let s = &mut stack[cur_stack];
match (s.pop(), s.pop(), s.pop()) {
(Some(Token::Num(right)), Some(op), Some(Token::Num(left))) => match op {
Token::Add => stack[cur_stack].push(Token::Num(left + right)),
Token::Mul => stack[cur_stack].push(Token::Num(left * right)),
d => panic!(format!("unexpected op {:?}", d)),
},
d => panic!(format!("unexpected trio from on stack: {:?}", d)),
}
}
}
Token::Space => unreachable!("no space should be present"),
};
}
match stack[cur_stack].last() {
Some(Token::Num(n)) => *n,
d => panic!(format!("Unexpected stack contents: {:?}", d)),
}
}
fn parse_part2(tokens: &[Token]) -> u64 {
let mut stack = vec![Vec::new()]; let mut stack = vec![Vec::new()];
let mut cur_stack = 0; let mut cur_stack = 0;
tokens.iter().for_each(|t| { tokens.iter().for_each(|t| {
@ -133,7 +228,12 @@ fn parse(tokens: &[Token]) -> u64 {
#[aoc(day18, part1)] #[aoc(day18, part1)]
fn solution1(tokens_list: &[Vec<Token>]) -> u64 { fn solution1(tokens_list: &[Vec<Token>]) -> u64 {
tokens_list.iter().map(|tokens| parse(tokens)).sum() tokens_list.iter().map(|tokens| parse_part1(tokens)).sum()
}
#[aoc(day18, part2)]
fn solution2(tokens_list: &[Vec<Token>]) -> u64 {
tokens_list.iter().map(|tokens| parse_part2(tokens)).sum()
} }
#[cfg(test)] #[cfg(test)]
@ -158,7 +258,6 @@ mod tests {
#[test] #[test]
fn part1() { fn part1() {
// 1106240272 too low
for (input, want) in vec![ for (input, want) in vec![
("1 + 2 * 3", 9), ("1 + 2 * 3", 9),
("(1 + 2) * 3", 9), ("(1 + 2) * 3", 9),
@ -171,10 +270,8 @@ mod tests {
("5 + (8 * 3 + 9 + 3 * 4 * 3)", 437), ("5 + (8 * 3 + 9 + 3 * 4 * 3)", 437),
("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))", 12240), ("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))", 12240),
("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2", 13632), ("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2", 13632),
("((9 * 7 + 6 + 7 * 9 + 2) * 4 + 3 + 4 + 4 * 6) * 8 + (4 + (8 + 2 * 6 * 4 * 3)) + (5 + 2 * 6 * (4 * 5 + 4 + 2)) + 4 * (6 + 3 * (2 * 2 * 6 * 3) + 8 + 4 * 5)", 1740572064000),
] { ] {
let got = parse(&lex(input)); let got = parse_part1(&lex(input));
eprintln!("got {}, want {} for {}", got, want, input);
assert_eq!(got, want, "got {}, want {} for {}", got, want, input); assert_eq!(got, want, "got {}, want {} for {}", got, want, input);
} }
let input = r#"1 + 2 * 3 + 4 * 5 + 6 let input = r#"1 + 2 * 3 + 4 * 5 + 6
@ -184,9 +281,405 @@ mod tests {
((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"#; ((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2"#;
assert_eq!(solution1(&generator(input)), 71 + 26 + 437 + 12240 + 13632); assert_eq!(solution1(&generator(input)), 71 + 26 + 437 + 12240 + 13632);
} }
#[test]
fn part2() {
for (input, want) in vec![
("1 + (2 * 3) + (4 * (5 + 6))", 51),
("2 * 3 + (4 * 5)", 46),
("5 + (8 * 3 + 9 + 3 * 4 * 3)", 1445),
("5 * 9 * (7 * 3 * 3 + 9 * 3 + (8 + 6 * 4))", 669060),
("((2 + 4 * 9) * (6 + 9 * 8 + 6) + 6) + 2 + 4 * 2)", 23340),
] {
let got = parse_part2(&lex(input));
assert_eq!(got, want, "got {}, want {} for {}", got, want, input);
}
}
#[test]
fn comprehensive_part1() {
let mut ts = vec![
("(7 * (3 + 8 + 8 + 7) + (6 + 8 * 2 + 5 + 2 * 6) * (5 + 2) * 9) + ((7 * 4 + 8) * 6 * 8 + 9) * 7 * 2 * 2", 740124),
("6 * ((9 + 4) * (6 * 7 + 5 + 8 * 2))", 8580),
("7 * 8 + 2 + 8 * (8 * 4) * (4 + 8)", 25344),
("(9 + 3 + 2 * 5 * 8) + 9 + 5 * 2 * 5 * (6 * 6 * 4 + 6 * 9 * 3)", 23247000),
("6 * 6 * 4 * (6 + (3 * 9 * 2) + 9 + (4 + 7 + 7))", 12528),
("7 * ((4 * 6 + 4 + 6 * 8 + 6) + (2 * 7 * 8 + 5 * 3 + 7) + (5 * 5 * 5 * 7) + 8 * 7)", 74431),
("(8 + 9 * 7 * 9 + 6) + (7 + 5) + 6", 1095),
("5 * (6 * (3 + 2 + 9)) + 8 + 3 + 5 * (4 + 3 * 8 * 8 * 6 * 2)", 2343936),
("4 + (3 + 4 * (4 + 9 + 3) * (4 * 8 * 4 * 9))", 129028),
("4 * 4 * 6 * 2 + ((8 * 4 * 3 + 6) * 5) + 6", 708),
("(8 * (8 + 5 + 5 * 7) * 9 + 2 + 9) * 4 + 9 * ((3 + 3) + 8) + 9", 508783),
("((9 + 8 * 3 + 2 + 9 + 8) * 3 + (3 + 7 * 6) * 9 * 2) + 3 * 7", 34041),
("5 + 5 * (5 * (6 + 6 + 6 + 7) * 7 + (9 * 4) * 4 * (8 * 9))", 2623680),
("7 * (2 * 7 + 3 + 5 + 5 * 2) + 4 + 3 * 3 + 5", 1160),
("(4 + 5 * (2 * 4 + 5 * 2 * 7 + 5) + 2 + 9) + 4 + 9 * 9 * 3", 46089),
("7 * 6 + (6 * 2 + 4 + 5 * 8) + (6 * 2 * (4 * 7 * 2) + 9 * (5 * 2 * 4) + (3 + 2 * 5)) * 8", 219800),
("2 + ((9 * 3 * 3 * 3) * (5 * 9 + 6 + 7 + 6 + 8) * 3)", 52490),
("7 * ((9 + 7 + 4 * 3 * 4 + 4) + 4 * 3) * 5 * 3 * 7", 546840),
("8 * (3 + 5 * 9 + 4) * 6 + 4", 3652),
("7 + 6 * 7 * ((7 * 6 * 2) * 3) * 5 * 7", 802620),
("8 + 5 * 4 * (3 + 5 * (8 + 5 * 2) + 6) + 6", 11134),
("4 * 8 + ((3 * 5 * 4) + 7) + (4 * 2)", 107),
("((5 + 8 + 7 + 6 * 6 + 3) + 4 * 3 * (5 * 2) * 5 * (2 * 9)) + 6 + 3 * (2 * (2 + 4 + 8) * 4) + (9 + 3 + 6 * (9 + 5 * 6 * 3 * 8 * 5) + (4 * 6) + (6 * 3 + 2 + 8))", 49473700),
("(3 + 4 + 8) + 6 * (7 * 6 * (9 * 3 * 5 * 5 * 4 * 5) + (6 + 4) * (5 * 2 * 3 + 3) + (2 * 7)) + 2 * 4", 1571752904),
("6 * (6 * 9 * (3 * 9) * 4 + 9) + 6", 35052),
("4 + 6 * 7 * ((3 + 5 + 3) * (6 + 3 * 9 + 6 * 8 * 9) + 6 * 4) * 8", 154358400),
("5 + (6 + 9 * (7 * 8 * 8 * 3 * 4) + 2) * 4 * 2", 645176),
("(2 * 7 * 6 + 9 + (3 * 6 + 4 * 7 + 5) + 8) + 7 + 7 * 8", 2192),
("2 + (6 + 4 + (5 + 5) * 7) * 2 + 7 * 5", 1455),
("4 + 9 * 3 + 8 * 6", 282),
("(5 + 5 + 9 * 8 * (4 * 5 + 6 + 3 * 8 * 3) + 6) + 9 + 9", 105816),
("(8 * 4 * 7 + 9 + 6 * 9) * (9 + 2 * 2) + 3", 47325),
("((9 * 4 + 8 + 4 * 2 + 2) * 3 + (6 + 9 * 5 * 4) * 4 + 6 * 5) * 5 * 3 * 5 + (3 + 5 * 4)", 893282),
("5 + (5 * 3) * (2 + 8) * (3 + 6 + 4 * 6 + 7 + 7) * 5 * 7", 644000),
("6 * 6 * 7 * (5 * 4 + 2 + 6 + (8 + 6 + 4 + 9 * 5))", 41076),
("2 * 9 + (2 * (8 * 7 + 6 + 5 + 3) + 2 + 9 * (3 * 9 * 4 * 7 * 7 + 5) + 3) + (4 + 6 * 4 + 6)", 799914),
("(9 + (8 * 8 + 2 + 2 * 6 + 6)) * 8 + 4 + 3 + (9 + 3 + 9 + 9 * 4 * 8) * 2", 8702),
("8 * 4 * (4 * (3 + 7 * 7 + 3) + 8 * (5 + 2 + 3) * 2) + 9", 192009),
("5 + (6 * (3 * 3 + 7 * 3) * (4 * 4) + 2 * 4 * 4)", 73765),
("3 + (5 * 8 * 9 * 3 + 9 * (6 * 7)) * 9 + 3", 411672),
("3 * (9 * 3 + (5 + 4 * 7 + 3 + 8)) + 4 * 2 * 3", 1842),
("8 * 9 + 6 + (7 + 7 + 9 * 8 * 5 * 6) * 5", 27990),
("(8 + 9 + 3 + 7 * 7) + 2 + 9 + (8 + 7 + 4 + 2) + 9", 230),
("4 * (2 * (7 * 4 + 2 + 9 + 6 + 7) + (4 + 2) * 3 + 9 + 6) + 6 * 4 + 7 * 5", 27755),
("(3 * 4 + (5 * 8 * 2) * (7 + 8)) + 7 + 8 + 5 + ((6 * 9) * 4 * 5)", 2480),
("4 + ((9 + 9) + (7 * 8 + 2 * 8 * 2) + 9) + 2 + 7 * 2", 1936),
("8 * 3 * (9 + 5 + 8 + 5)", 648),
("4 + 4 + (6 + 2 * 7 + 9 + 6 * 5)", 363),
("((8 + 3 * 8 * 2) + 7 + 7 * (7 * 7 * 7 + 3) * 9 * (9 * 4 + 7)) + 4 + 6", 25441390),
("6 + (7 * 2 + 4 + 5 + 4) + 9 + ((7 * 9) * 7) * 5", 2415),
("(7 * 3 * 2) * ((9 + 6 * 2 * 6 * 3) + (6 * 7) + 3 + (6 + 5 * 5 + 4 + 9 * 6) * 4 + 9) + 4 + 7 + 6", 167219),
("9 * (3 * (9 + 8)) * 4", 1836),
("6 * (4 * 2 * (9 * 5 * 4 + 7 + 9) + 6 + (3 + 6 * 2)) * 5 * (3 * (2 + 6 * 8 * 6 + 3 + 7) * 8 * 7 * (9 * 8 * 7 + 7 + 9) * 7) + 3 + 6", 11507240908809),
("7 + 3 * ((6 * 3 * 9) + 3 * 9)", 14850),
("7 * ((4 + 4 * 4 * 9 * 7) + 2 * 9) * 8 * 6 * 6 + 3", 36614595),
("6 + 5 + (5 + 3 * (3 + 7 + 9 * 3) * 6) + 3 + ((2 * 3 * 4 + 4) * 6 + (3 + 3) + 2)", 2926),
("9 + ((8 * 9 * 3 * 2) * 5 + 8)", 2177),
("(2 + 7) * 3 * (8 + 5) + 9", 360),
("4 * (5 + 4 + 6 * (9 * 4 + 8 * 4 * 7 * 3) * (4 * 4 * 6) * (9 + 7 + 8 * 4)) * (4 + 4 + 9 + 8) + 7 * 8 + 5", 408748032061),
("((7 * 4 * 4 * 3) * 4 + 2 * 2 + 2) * 9 + 9 * 8 + 9", 194049),
("4 + 6 + 2 * (5 + 3 + 8 * 4)", 768),
("((7 + 6) + 3 * 3 * 5) * ((9 * 3 + 5 + 8) + (3 + 8 * 8 + 7 + 5 * 6) + 2 * 5 + (7 + 9 + 2)) + (4 * 2 * (5 + 8 + 5 + 3) * 5 + 6 * 2) + 6 * 5 * (2 * (9 + 4) * (7 * 8) * 8 + 7)", 45245758950),
("6 + 5 + 5 + 2 * 5", 90),
("5 + 5 + 8 * 8 + 2 + 6", 152),
("(3 * (4 * 6 * 8 * 2)) + 3", 1155),
("(5 + 6 * 3 + 5 * 2) * 5 * (3 * 5 + 2 * 7 * 9 * 3) + 5 * ((6 + 2 * 7 + 2 + 4 + 2) * 9 + 6) + 7", 710589997),
("(7 + 7 * 4 * 7) * ((8 * 5 * 9) + 6 * 9 + 3 * 3 * 9) * 8 * ((3 * 5 * 2 + 6 + 5) * 7 + (6 * 2 * 7 * 7) + 8 + 8 + 6)", 250409734848),
("(3 * 6 + 7 + 6 + 3 + 7) * 4 * 7 * ((9 + 3 + 6) * 5 + 3 + (5 * 7 + 4 + 5 * 2) + 5) * 5 * 9", 9608760),
("7 * ((7 + 6 + 2 + 9) + 3 * 4 + 9 + 2 + 2) + 2", 849),
("3 * 8 * 2 * (6 * 2 * 2 + 4 * 8) + 8 + 7", 10767),
("8 + 3 + ((4 * 6 * 5 * 7) * 9 + 9 + 8 * 4)", 30319),
("(6 + 4 + (6 + 3 + 3 + 3) + 6) * 9", 279),
("2 * 6 * (5 + 2) * (6 * 6 + 7 + 2)", 3780),
("4 + (8 + 2 + 4) * 4 * 5 + (9 * 5 + 9) + 8", 422),
("6 * 3 * 9 + 6 + (2 * 4 * 3 + 2 * 5 * 3) * 6", 3348),
("2 + (8 + 5 * 7 + 4 + 7 * (2 + 9 * 6 * 5 * 3 + 3)) + 9 * 4", 405188),
("(9 + (2 + 6 + 7 * 4 * 8) + 9 * 4 + 9) * 3 + 8 * 8", 48088),
("2 * (9 * 5 * 9 + (8 * 9 + 5)) * 8 * 8", 61696),
("3 + 6 + 2 * 3 * 9 + 9", 306),
("8 * 7 + 8 * (5 + 6) * 5 + 4", 3524),
("4 * (6 * 2 * (3 + 6 * 3) + 7 * 5 + 6)", 6644),
("(7 * 3 * 7 * 4 * 3 * 2) * (9 + 8 * 9) + 9 * (4 * 5 + 3 + 5 + 9)", 19972341),
("9 + 8 * 3 * 4 + (9 + 8 * 9) + (4 * (6 * 4 * 9 * 5 + 6 + 4) + 8 + 7 + (4 * 9))", 4768),
("2 * (4 * (3 + 4 + 8 + 5) * 8 * 6)", 7680),
("8 * (8 * 2 * 6 + 6 + 6) * 8", 6912),
("(9 + 3 * 4 * 9 + 5 * 5) * 4", 8740),
("(9 * 9 * 4 * 4 + (6 * 2) + (3 + 3 * 2)) + 3 + ((2 * 3 + 8 * 4 * 8 * 2) + 2 + 3)", 2224),
("7 * (9 * (6 + 3 + 7)) * ((8 + 3 + 3 * 3 + 3 + 7) + 4 + 5 + 3 * (6 * 4 * 6 * 6 + 2 * 5) + (2 + 3 + 9 + 3 * 6 + 4)) + 6", 279443814),
("8 + 6 * (3 + 5 + 6 + 9) * 4", 1288),
("(6 + 7 + 7 + 7 * 6 + 5) + 3 * 5 * 4 * 5 * 6", 102000),
("(2 * 9 + 9 + 3 * 7 + 6) + 3 + 7 + 4 + 9", 239),
("(7 + (8 * 7 * 4 * 7 * 2 + 4) + (5 + 2 * 2 + 2 * 2 * 6)) * 3 * 4 + (4 * 2 + 4 * 6 + 3 * (8 * 6 + 4 * 3 * 7)) * 6", 731808),
("2 * (3 * (5 * 7 * 8 * 7 + 8) + (6 * 3 + 5) + 5 + 9)", 11882),
("8 * 4 + ((7 + 2 + 8) + (2 * 8 * 4 * 4 + 4 * 7) * 2) * 5 * 4", 74120),
("2 * 6 * 2", 24),
("2 + (2 + 6 * 5 * 9) + ((8 + 2 * 6 * 5 * 5) * 7 + 3) + 3", 10868),
("((9 + 4 + 6 * 3 + 9) * 5) * (7 * 2 * 7 * 5 * 2) + 7 + 9 + 7", 323423),
("2 + 2 * 8 + 8", 40),
("4 + 6 + 4 * 7", 98),
("4 + 5 + 5 * 8 * (8 + 4 + (2 * 8 + 9 * 9 + 4 + 5))", 27552),
("(7 + 2 + (8 * 4 * 2 + 9 * 6) * 4) * 2 + (9 * 2 * 8 * 9 * 4)", 8760),
("4 + 9 + 6 * (4 * 6) + 7", 463),
("6 + 4 + 8 * (7 * 9 + 7)", 1260),
("3 * 2 + 2 + 9 + (2 * 9 * 2 * 8 * 2 * (2 + 8 + 9 * 9 + 7)) * 7", 717815),
("9 + 9 + 5", 23),
("(2 + 3 * 3) + 7", 22),
("(9 * 7 + (8 + 8 * 6) + 2 * 7 * 3) + 6 * (5 * (2 + 9 + 9 + 6) + 6) * 8 * (4 + 4 * 6 * 5 + 5) * 8", 7222709760),
("7 + 4 + (4 * 2 + (2 * 7 + 2 + 4 + 2 + 5) + 9 * (9 * 7 + 7)) + 9 + 7 + (8 + 9 * 7 + 2 + (7 * 8 * 5 * 4 + 6 + 8))", 4362),
("(8 * 3) * (9 + 2 * 7 * 7 + 7) * 6 * 8 + 8 + (9 + 9 + 3 * 5 + 3)", 629108),
("4 * 5 + 2 + 4", 26),
("((7 + 3 * 8 + 3) * 8 + 4 + 4 * 4 + 8) * 5 + 9 * (3 * 9 + 8 * 7 * 9 * 4) + 7", 118972987),
("(8 + 6 + 7 + 9) + 8 * 6 * (5 + 2 + 3) * 8", 18240),
("(9 * 8 * (6 * 5) + 9) + 4 + 8 + 8 * 8", 17512),
("(4 + 5 * 9) * 7 + 7 * 6 + 8 * (2 + 5 * 8)", 193312),
("(4 + 3 + (6 + 2 + 2 + 6 * 6 * 2) * 4 * 2 * 9) + 3", 14331),
("8 * (5 + 8 + 2 * 7) + 2 + ((5 * 6 + 4 + 7 + 6 * 5) * 7 * 3 + 8)", 5785),
("2 + 5 + (6 * 3 + 2) + 8", 35),
("2 + 9 * 6 * 5 + 5", 335),
("3 * 3 + 7", 16),
("(9 + 4 * 9) + 5 + (2 * 6 * (6 + 9) + 2 + 6) * 2 * ((9 * 6 + 7 * 3) + 5 * 2 + 3 * 9)", 2114820),
("2 + 6 + ((6 * 9 * 9 * 8 * 9) + 9) * (5 + 2 * 5 * (3 + 5 * 8 * 5 * 2 * 9) * 8) * ((7 + 9 + 3 + 6 + 5) * 4 * 5)", 33877509120000),
("3 * 9 * (5 + (3 * 4 * 2 + 8) + 5 * 3) + 4", 3406),
("9 * (9 * 3 + 3 + 4 + 7 + 4) + 3", 408),
("2 * 9 + 3 * 9 + 5 + 9", 203),
("(6 + (6 * 6 * 6 + 7 + 7 + 5) * 5) + 3 + 4 * 9", 10908),
("(2 * 8 * 8 * 6 * 8) + (5 + (5 * 6) * (8 + 5 + 7 * 7 + 2) + (6 * 4 + 7 + 5) + (3 + 5 + 8 + 6)) * 8 + ((8 * 3 + 3 * 7) * 4 + (3 * 8 * 4))", 90228),
("4 + ((9 * 2 * 9 * 9 * 7 + 5) * 6) * 7 * 7 + 5 * 8", 24017880),
("(3 * 6 + 6 * (5 + 6 * 4 * 6 + 2 + 9)) * 9 * 9 + (2 * 7 * 2 * 5)", 534740),
("6 + 2 + (8 + 6)", 22),
("6 + 3 * (3 + 3) + 8 * (9 * 6)", 3348),
("(2 * 8 + 4 + 5) + 7", 32),
("7 + (2 * 9 * 5 * 6)", 547),
("2 + 9 + ((7 + 3 + 3 * 9) * 6 + (5 + 2 + 8 + 5 * 6) * 7 + 8)", 5773),
("((4 + 9 + 8 * 6 + 8 * 9) + 8) * 6", 7284),
("9 * (9 * 3 + (3 * 8 * 8 * 7) * (4 + 8) + 8 * (5 * 3 + 2 + 4 * 3))", 9332820),
("9 * (7 + (3 * 5 * 5 + 3 * 7)) + (5 * 3 + (4 * 7 * 7 + 8) * (3 * 7 * 5 * 6) * 8) * 5 + (2 * 7) * 4", 22174796),
("4 * ((2 + 3 * 2 * 2 + 3) + 4 * 4 * 4 * 5 + (3 + 3 * 9 * 9 + 7)) * 9", 95508),
("8 + 7 * 4 * 3 * (5 + 9 + 8 * 7 * 3)", 83160),
("9 + (4 * (2 * 6 + 7) + 5 * 4 + 8 + (6 + 7 * 8 * 2)) + 9 + 5 + 8", 571),
("3 * ((5 * 2 * 8 * 7 * 2 + 4) * (3 * 3 * 5 + 2) + 7 + 8) * 8 * 2 + 3", 2536467),
("(8 * 2 + 5 + (4 * 9 * 7)) * 5 + (9 * 8) * (5 * (7 * 8 + 4 * 4) + 3)", 1728711),
("((7 * 6 + 9 + 7 + 9 + 9) * 2 * 8 + (6 + 4 * 7 * 2 * 2) * 7 + (2 + 9 + 9 + 5 + 4)) * 5 * 2 + 7", 105017),
("7 + 2", 9),
("4 * ((2 * 6) * 5 * 5 + 9 * (7 * 7 * 3 + 8)) + 2", 191582),
("6 * 8 + 5 + 4 * (5 + 8)", 741),
("4 + 6 * (4 * 5 * 3 * 5 * 5 * 6) * 8 * 5", 3600000),
("7 + ((6 * 6 + 2) + 7 * 7 + 7 * 9 * 6) + 6", 17401),
("(7 + 3 * (2 * 5 + 3 * 9)) + 2 * 2", 2344),
("6 + 3", 9),
("2 * 8 * (3 + 8 * (4 + 6 * 4) * 8 * 2)", 112640),
("8 * ((4 + 8 * 5 + 7) + 6 + (4 + 8 + 4 + 9 * 7 + 9) + 2) + 5", 2077),
("(7 + 7 + 2 + 3 + 6 * 3) * ((7 + 5 + 8 + 5 * 6) + 4 + 6 * (3 + 3) * (4 + 6 * 9 * 6 + 4) * 7) * 3 * 9 + 5 + 4", 7402752009),
("5 * 7 * (5 * (2 + 5 * 7 + 7 * 5) + 9) + 3", 49318),
("8 + 2 * 4 * 3 + (8 + 3 + 4 + 3 + 2 * (4 * 5)) * 5", 2600),
("3 + (8 * 3) * 7 + 4 * 5 * 2", 1930),
("(7 * (7 * 2 * 7 * 3) * 3 + 2 + 7 * 6) * 4 * 6 + 6 + 6", 890364),
("(5 + 2 + 2 * 8 + (2 * 9 + 7 + 3) + 5) * 9", 945),
("(6 + (7 + 8 * 2 + 9) + (7 * 3 * 4 * 5) + 6 * 3 * 4) + (3 * 2 * 5)", 5682),
("4 + 7 + (8 * 7 + (5 * 4))", 87),
("8 * 2 * 5 * (9 + 4) + (3 * 7)", 1061),
("(3 + 4 + 8 + 6 + 2) * 9 + 6 + 6 + 3", 222),
("(9 + (3 * 4 * 5 * 8 + 5) + (5 + 8)) + 5 * ((2 + 5) + (7 + 9) * 2) * (6 * 3) + 8", 423944),
("9 * (3 + 9 + 8 * (9 + 7 + 9 + 8 * 6) + 8 * 8) + 2", 285698),
("7 * ((9 * 2 + 8 * 4 + 7) * 4 + 3 + (5 * 3 * 5 + 3 + 4)) * 5 * 7", 129605),
("5 + 2 + (8 * 8) + 8", 79),
("4 * (4 * 9 + (7 * 6 + 2 * 2 + 5 + 8) * 9 + 7) * 3 * 7", 104160),
("9 + 9 + (4 * (8 + 2 * 5 * 9 + 9 * 2) * 4 * 7) + (6 + 2) * ((8 * 7) + 9 * (7 + 7 * 7) + 2 + 5 + 2) + 6", 656029124),
("9 + 4 + (3 * 9 * 3 * 9) + (3 + (2 + 9 + 9 + 8 + 7) + 5 + 9 * 3) * 7 * (4 + 7 * 8 + 7 * (4 * 2 + 5 * 8 + 9 + 8) + 3)", 72276428),
("6 + (2 + 6) * (6 * 2 * 2 + 7) * 4 * 6", 10416),
("6 * (2 + (6 * 7 + 7 * 9) * 6) * 3 * 8 + 3", 382755),
("8 + (5 + 4 + 6)", 23),
("9 + 2 * (5 * 5 * 5) * 2 * 5", 13750),
("4 * 4 * ((3 * 3) * (9 + 7 + 5) + 5 + 9) + 7 * (4 * 2 * 4) * 7", 729120),
("((4 * 5 * 7 * 7 + 3) * 9) * (6 * (7 * 8 * 7 + 3 + 7) + (4 * 2 + 3 + 3) * 4 + 6)", 85904370),
("4 * 2 + 3 + 2 * (2 + 2 * 2 * 8)", 832),
("4 + ((3 + 8) * 7 + 2 + (9 + 4 + 9 + 3 + 4 + 6) * (7 * 5 + 8)) + 4 + 5 * 8 + 2", 39322),
("(5 * 2) + 3 + (3 + 9) + 4 + 4 * 5", 165),
("6 + (5 + 8 + 6 + (7 + 5 + 9)) + (3 * 5 + 8 * 3 * 4) * 4 + 4 + 5", 1297),
("((7 + 4 * 6) * 7 * 2 + 6) * 7 + 8", 6518),
("7 * 5", 35),
("2 + 9 * 4 + 9 + 4 + (8 * 9)", 129),
("5 + 5 * ((5 + 6) + 2 * 4) * (8 + 8 * 5) + 3", 41603),
("5 * 5", 25),
("4 * 3 * 4 + 3 * (7 * 9) + 4", 3217),
("4 + 4 * (8 * 3 + 2 * 8 + 2) + 4 + 3", 1687),
("(8 + 5 + 4 * 4 + 6 * 9) + 8 * 6 * 3", 12132),
("8 + 6 + 3 + ((2 * 9 + 2) + (7 + 7 + 3)) + 6", 60),
("(4 + 5 + 8) * 8 + (7 * 9 + 3 * 3 + 9 + (8 + 5 + 7 + 3 + 9)) + (3 * 3) * 8", 3072),
("(4 + 4 + 4 * 6 + 4 + 9) * 6 * ((8 + 6 + 3 + 2 * 8 + 2) * 4 + 6 * 8 * 7) * 5", 88821600),
("6 * 6 + 3 * (2 + 7 + 3) * 4 + 5", 1877),
("((2 * 4 + 3 * 3 * 9 * 3) * 7) + 2 + 8 + 6 * 2", 12506),
("5 * (5 * 4) + 6", 106),
("((7 + 6) + 7) + 3 * 2 * 9 * 6 + 4", 2488),
("6 + 8 + ((4 * 9 + 5) * 4) * (5 + 7 + 8 * 8 * 2) + 9", 56969),
("8 * 4 * 8 * 9 * 2", 4608),
("5 + 2 + (8 + 9 * 6 * 7 * 6) * 8", 34328),
("6 * (9 * 5) + (5 + 2 + 6 * 8) * 6 * 6 * (4 + 7 + 8 * 8)", 2046528),
("2 * 3 * 6 * 8", 288),
("3 + (4 + 5 * 8 * 8) * 2 * (9 * (5 * 3 + 8 + 6) * 2 + (5 * 7 * 3) * (4 + 5) + 7)", 6542700),
("6 * (5 * 4) * 5 + 4 + (6 + 2) + 6", 618),
("2 * (4 * (7 + 2 + 3 + 3 * 8) * (9 + 3 * 3) + (6 * 8 * 5 * 7) + (6 + 5) + 7) * (3 + 8 + (5 + 4 + 8 + 6) * 3 + 2 + 3) * 8 + (3 * 3) * 9", 292413105),
("(5 + (4 * 8 * 7) + 7 * (7 * 9 + 2 + 5 + 6)) + 4", 17940),
("6 + ((3 + 9) * 4)", 54),
("9 + ((4 + 4 + 5 * 6) * 8) * 2", 1266),
("(4 + 2) * 3 * 3 + 3", 57),
("5 + 2 + (8 + 7 + 5)", 27),
("6 + (7 + 4 * 9 * (6 + 8 * 8 + 3) * 8)", 91086),
("3 + 3 * 6 * 3 + (4 * (2 + 6 * 7) * 8)", 1900),
("7 * 5 + 8 * 7 + (9 + 4 * (7 + 6 + 2 * 7 * 9) + 9 + 9 * 4) + (3 + 6 + 5 * 6 * 4 + 9)", 49858),
("5 + (9 * (5 * 7 * 6 * 8) + (8 * 3) * 6 + (2 + 7 * 8)) * (9 + (5 + 7 * 2 * 6 + 9 + 7) + 4 + 8 * 3) * 7", 345666741),
("(6 + 7 * 7 * 9 * 4) * 3 + (5 + 7) * 7 * 5 * 6", 2066400),
("(9 + 6 * 5 + 3) * (2 + 6 + 6) + 4 * 6 + 3", 6579),
("5 + 8 + ((9 + 8) * (3 * 3 + 8 * 8) * 2 + 3) * 6 * 4", 111360),
("((3 + 9 * 2 * 8 * 7) * 7 * 8 + 3 + 7) * (8 * 9) * 5", 27098640),
("5 + 3 + (4 * 4 * (8 * 8 + 4 * 4) + 4 + 5 * 6) * 4 + 2 + 7", 104705),
("5 + ((6 + 4 + 9 + 3) * 6 + (2 + 4 * 9 + 4 + 7 + 7)) + 4 + 9 * 8", 1776),
("2 + 4 * 7 * 5 * ((5 + 4 + 6) + 4 * 4)", 15960),
("8 * 6 + (4 + 8 * 5 * 6 + 4 * 9) * 9 + (2 + 8)", 29926),
("((8 + 3 * 2 * 6) * (7 + 8 * 8) * 9 + (2 + 5) + 4) * (2 * 9 * 5 + (9 * 4 * 2 + 7 + 8) * 2 + 2) * 6", 304531656),
("4 * ((5 * 5 + 6 * 8 * 5 * 9) + 4 * (3 + 7 * 6 + 4) * 4) + 2 * ((9 + 8 + 9) + 9 * 2) * 8 + 4", 6401885284),
("2 * 8 * 6", 96),
("(7 + (7 + 8 * 9) * 7 + (5 * 8 * 6 * 9 + 6 * 4)) + ((5 + 9 * 6 + 7) + 8 + 7 + (8 * 7 * 4 + 5 + 2) * 5 + (9 + 3 + 9 * 2 * 6 * 3)) * (4 + 5 * 4) + (8 * (5 * 5) * 9 * (3 * 3) + 5) * 4", 1807076),
("(6 + 5 + 5 * 5) * 4 + ((8 + 6 + 8) + 5 + 5 + 7 * 9)", 671),
("9 * (9 * 2 + 4 * 2 * 4)", 1584),
("4 + (6 + 2) * (4 + 2 + 2 * 5) + 2", 482),
("5 * (6 + 8 + 5 * 2 * (7 * 2 + 2))", 3040),
("((7 + 5) * 2 * (3 * 5 * 9)) + 7 * (7 * 5 + 9 * 9 * (5 * 9))", 57861540),
("7 + 5 * (2 * 3 * 4 + 9 * 7 * 9) + (2 + 8 + (3 * 6) * 7 * 7 * (5 * 8 * 7 + 8 * 6 * 9)) * 5", 106811460),
("9 * 3 * ((9 + 7) * 7 * (2 * 9 + 5 * 9 * 7 + 8) + 6 * 6 + 6) * (9 + 7 + 6 + 9 * 7)", 5736816414),
("8 + 9 * 4 * ((7 * 3 + 6 + 6 * 7 + 9) + 8 * 5 * 2)", 168640),
("3 * (3 * (5 + 5 + 5 * 2))", 270),
("4 * 4 * 8 * 6 + (3 + (5 + 5 + 5 + 6 * 6) * 8 * 9 + 9) + 3", 10068),
("(3 + 8) + 4", 15),
("6 + 5 + 3 + ((2 + 5 + 6) + (9 + 5 * 4 * 9) + 6 * 7 + 5)", 3680),
("(6 * 8 * (2 + 2 + 8 + 8 + 2) + 7 + 6 * 3) + ((7 * 3 * 9) + 2 * 4 * 4 + 2) + 5", 6270),
("6 * ((6 * 8 + 3 * 9) + 2 * 9) + 3 * (6 + (3 * 7 * 4) + 4 * (6 * 8 * 3 * 7 + 3 * 8) * 6 * 7) * 6", 4769979979968),
("5 * 8 * (7 * 7 + 4 * 7) + (2 * 4 + (3 * 2 * 4 * 9) + 4 + 2)", 15070),
("(3 * 8) + 3 + 6 + (3 * 3 + 9 * 6 * 8) + 3 + 5", 905),
("((6 + 5 + 7 + 3) * 7 + 9 * 5) + 9 + 2", 791),
("3 + 2 * ((9 + 9) * 8 + 5 * (6 + 4 * 6) + 4 + 7) + 9 + 4 * 4", 179072),
("5 * 6 + (5 + 6 * 8 * 5 * (8 * 2 + 7) * (7 * 6 * 2 * 2 * 7 * 6))", 71406750),
("8 + 4 + 3 + (4 * 7) * 2 * (2 + (3 + 8 + 8 * 2 * 9))", 29584),
("7 + 5 + 3 + ((9 * 9 + 2 * 8 * 6 + 5) * 2) + (3 * 7 + 6 * 4 * (8 * 6 + 8 + 8 + 7 * 3) + (9 + 3 * 8 * 6 + 4)) * (9 * 6 * 9 * 6)", 92078532),
("5 + (2 + (6 * 3 * 3 + 9 * 8 * 8) + 3) + 9 * 6 + 6", 24312),
("6 + (2 + (3 * 5 + 2 + 4)) + 7 * 9 * (8 * 9 * 6)", 139968),
("((4 * 4) + 2 + 7 + (5 + 3 * 2 + 9 + 8 + 4) * 5) * (2 * (6 + 8 * 9 + 7) * 9 + (7 + 5 * 7) * 8) + 6 * 3 * 3", 55309014),
("(7 * 5) * (5 + 3 + 5)", 455),
("(2 * 6 * 9 + 9 + 6) + 9 * 8", 1056),
("6 + (9 + 6 + 7 * 7 * 3 + (9 + 8 + 2 * 3 + 4 + 7)) + 9 * ((2 * 5 + 4 * 6 * 6 * 7) + (6 + 5 * 4 * 5 * 8) + 9) + ((5 + 7) * 5 * 4)", 2887105),
("(7 * 2 + 7 * 8 * (7 + 6)) * 2 * 2 * 2", 17472),
("(6 + 5 * 2 + 3 + 5) * 8 + 4 + 2", 246),
("3 + 4 + 3 + 7 * ((2 * 8 * 8 * 3 + 4 + 4) * 8 * 2 * 9 + 4 + 9)", 959837),
("7 + (5 * (7 * 9 + 7 + 2 * 7) + 9 + 3 + 5 + (8 * 9))", 2616),
("8 * (2 * (8 + 4 * 3) + 7 * 7 + 5 + 8) + 3 + 6 * 8", 36296),
("(8 + 3 * 5) * 6 + 2", 332),
("3 + 8 * ((3 * 8) + 8 + 5)", 407),
("5 * 9 * 5 + ((5 + 6 + 7) + 4) + ((5 + 8 + 3 + 4 + 3) * 8 * 3)", 799),
("7 * (6 + 4 + (7 + 4 + 4 + 6) * 3 * 3 + 2) + 7 + 8 + 4 + 3", 1989),
("2 + ((3 + 8) + 2 + 9 + 4 * 3)", 80),
("4 + 9 * 3 + 9", 48),
("(7 + (7 * 4 + 7) * (6 * 6 * 9 * 4) * (2 + 5 + 8 + 3)) * 3", 2939328),
("(8 + 2 + 4 * 7 * 3) + 2 * ((6 + 2 + 2) + 7 * 8 * 8 * 5 * 8) + 7 + (5 + 8 + 2 * (9 * 9) + 3)", 12883145),
("8 * 2 + 6 * ((5 * 3) * 6 * 2 * (7 * 2 + 4 * 4 + 9)) + 4", 320764),
("4 * 3 * (8 + 7 * (2 + 4 + 7 + 4 + 3)) * 9", 32400),
("7 + (6 + (6 + 2) + 8 + 9 + 3) * (3 + 9 * (3 + 5 + 6 + 7 + 6) + (8 * 8 * 7) + 5 + 8) * 9 * 2 * 8", 4634640),
("3 + 6 * (3 * 5) * (9 + 4 * 3) + (5 * 6 + 3) * 6", 31788),
("6 + (5 * 6 * 7 * (2 * 4) * 9) * 7 * 7 * 6 * ((2 * 6 * 2 + 3) + 7 + 9 * 2 + 6 * 6)", 2454768288),
("(4 * 4 * 2) * (4 + 3)", 224),
("7 * 8 * 9 * 6 * 2 + (8 * (8 + 6 + 6) + 3 + 5 + 2)", 6218),
("(9 + 2 + (8 * 9 * 5 + 7) + 3) * 9 * (7 + 8 + 3 * 4 + 5) + 8 * (5 * 5 + (4 + 5 + 5 * 3 + 8) * 4) + (7 + 3 + 7 + 4)", 79212321),
("8 * (2 * 2 + (8 + 7 * 9 * 2) * (8 * 2 + 5 + 4) * 9 + 8) * 6 + 3 + (4 * 3) * 7", 20717193),
("(5 + 7 + 9 * 6 + 4) * 9", 1170),
("(3 + (5 + 3 + 4) * 3) + 3 + 8 * ((5 * 8 * 6) + (8 + 6 * 4 * 4 + 8) * 8 + 2 * 4 + 7) + (2 * (5 * 5 * 3) * 2 * 9)", 849364),
("(8 + 2 * 8) + 9 * 8 + 7", 719),
("4 * (2 * 3 + 8 + 3 * 5) * ((3 * 3 * 8 + 7) * (7 + 5 + 8) * 6) + (9 * 5) + 8", 3223253),
("7 * (6 * 3) + (7 + 8 + (2 + 5) * 3) + 7 + 6", 205),
("5 + 9 * 2", 28),
("9 * 2 + 8 * (3 * (6 * 7 * 7) * 8) + 9", 183465),
("((2 + 8 + 7 + 6) + 4 * (6 * 5 + 7 * 4 + 5) * 8) * 9 + (7 + 2 * 5 * 5 * 3 * 3) + 3", 299460),
("6 + 3 * 3 + (8 + 4)", 39),
("(8 + (8 * 2) + 4) * 5 * 4 + 5", 565),
("(5 * 5 * 5 * 8 + 8 + 7) + (6 + 9) + 4 * 3 + 4", 3106),
("(3 + (2 * 9 + 2 + 9 + 4) + 2 + (5 + 3 + 2)) * 5 * 3 + 5", 725),
("6 + 3 * ((8 + 7 * 8 * 7 + 9) + 3 + 3 + 9 * (7 + 5 + 9) * 9) + 5 + (7 * 8 + (5 * 7 + 5 * 9 + 4))", 1470089),
("(4 * 9) + 7 * 9", 387),
("(3 + 7) + 2 + (5 * 2 * 4 * 6 + 5)", 257),
("5 * 4 * 2 * ((9 * 4 * 7) * 2 * 3) + 3 + 6", 60489),
("(5 + 2 * 7 * 7) * (4 * (3 * 8 * 2) * 3 * (2 + 3 * 8)) + 4 * 6 + 8 + 7", 47416359),
("7 * 4 + 2 + (6 + 2 * 5 * 2 + 6 * 9) + 7 * 2", 1622),
("8 + (4 * 4 * 7 * 9 + 5 + 6)", 1027),
("(4 + 7 + 8 * 6) + (3 * 5 + 4) + 4", 137),
("6 + 2 + 3 + 2 * 3 + 9", 48),
("6 + ((7 * 6 + 5) + 4 + 9 + (5 + 6 * 6 + 5 * 2 * 9) * (2 * 9)) + 9 + 5 * 2", 48208),
("8 + 3 * 4 * 3 * (3 * 9 * 2)", 7128),
("9 * (7 * 7 * 4) + 3 + (9 + (6 + 6 + 2 + 3 + 5 + 6) * (3 * 7 * 9 * 3) * 6 + 8 * 3)", 379413),
("5 * (9 * 2 + (6 + 2 * 5) + 5)", 315),
("4 + 3 + 9 + 7 + 5 + (3 * (2 + 8 * 6) * 4 + 2)", 750),
("3 * ((2 + 5 + 2 + 6) + 3 + 4 + (7 + 9) * (7 + 8 * 4 + 9) * 3) + 9 + (8 + 6)", 23621),
("8 + 4 * 4 + 7 + ((3 * 9 * 2 + 2 * 8) + (3 + 6 + 2 * 5 * 4 * 8) * 8 * 8)", 141367),
("4 + 4 * ((5 + 3 + 2 * 4 + 7 * 8) * (5 * 6 + 9) + 5 * 3)", 352056),
("7 * 8", 56),
("3 + (5 * 3 + (5 * 9) * 2 + 4) + 2 + 3", 132),
("6 + 8 + 3 + (2 * (6 + 5 + 3 * 8 + 9) * 3 * 9) * (6 * 8)", 314448),
("2 * 9 * 4 * (6 * 2 * 6 * 3 * 8) * 4", 497664),
("6 + (3 * 4 * 7 + 5 * 5 + 2) * 4 + (6 * 5 * (6 + 2 + 9 + 6 * 8) * 2 + 5) + 4", 12861),
("6 * ((6 + 4) + 9 * 5 * 3 + (9 * 5)) * (4 + 2 * 7 * (4 * 9 * 3 * 8) * 4) + 3", 287400963),
("7 * 2 + 6 * (3 * 6 + 6 * 5) * 7 * 7", 117600),
("3 + 8 * (3 * 2 + (9 * 3 + 8 * 7 + 8 + 4) + 6 + 3 + 5) + (6 + 8 + 7 * 3) * 9 * (4 * 3 * 6 + (9 + 9 * 9 * 2 * 6) + 4 + (6 * 4 + 5 + 9 + 9 + 7))", 58051260),
("4 * 8 * (8 * 5 + 3 * 6 * 7 + 9) * 9", 522720),
("8 + 4 * 9 * 4 * 8", 3456),
("(3 * 7 + (5 * 2 + 7 + 5 * 6) + 9 * 5) * 6 + 6 + 2 * 5", 24340),
("(8 + (4 * 8 * 8) * 7) + 4 * 8 * ((4 * 4 * 5) + 8 + 8 + 2) + ((6 * 8) * 8 * 8 * 8 * 2) + 8", 1501128),
("4 * (6 + 2 + 6 + 9 + 7) * 4 + 7 + 9", 496),
("5 + (2 + 6 * 8 + 5) + (6 + (8 + 9 + 5) * 4 * 3) * 4 * 5", 8200),
("9 * 6 + 5 * (4 + 2 + (7 + 9 * 6)) + 5 + 6", 6029),
("8 + ((8 + 3 + 5 + 3 * 8) + 5 + 3) * 4 * 3", 2016),
("2 + (3 + 5 * (8 * 2) + 8 + 5) * 8 + 4 + (8 + (9 + 8) + 8 * 9 * 7) + 9", 3236),
("7 * (4 * 2 + 5) * 8", 728),
("3 + 5 + 7 + (6 + 2 * 7 * 7 * 5) + ((3 * 4 + 8 * 5 + 6) + 4 * 5)", 2525),
("(9 + 6) * 7 * (9 + 3 + 4 * (8 + 8 * 3 + 6 + 8 * 2))", 208320),
("2 + (6 + 5 + (7 * 5 + 5 + 9 + 7 + 6) + 2 + 2 * (8 + 8))", 1234),
("4 * 9 + 2 * 9 + 9 + 8", 359),
("((2 * 7 * 5) * 8) * 8 + 4 + 8 * 4", 17968),
("7 * 5 + (8 + 9) * 3 * 6", 936),
("8 + ((9 + 7 + 8) + (8 * 5) + 2 + 7 + 3 + 3) + 3", 90),
("6 * (5 * (7 + 6 + 5 * 9 + 8 * 5) * 6) + 7 * 9 * ((2 + 5 * 8 * 2 + 3) * 3 + 7 + 5 * 3) * 2", 2949668946),
("3 * (3 + 5 + 4) * 6 * 9", 1944),
("6 + ((8 * 5 + 3 + 9) * (5 + 2 * 4 * 5 + 5) + (2 * 3) + 4 + 7) + 7 + 8 * 6 + 4", 45472),
("((9 * 2 + 8 * 9) * 4 + 5 * 3 * (8 * 2 * 7)) + (7 + 4 * 2 + 6) * 9", 2845836),
("(6 * 5 + 7 * (5 * 5 + 4 + 7 * 7 + 6) + 2) * ((4 + 6 * 5 * 6 + 7) + 9 + 5) * 7 * (4 + 7 * 6 + 5) * 5", 7616296380),
("8 * (3 * 6 + 5) + 9 * (9 + 6 * (4 * 6) + (7 + 8 + 5 + 3)) * (3 * 7 * 8 * 2) + 9", 24836793),
("8 * 3 + (5 + 3) + 8 * 4 + (4 + (7 + 9 * 5 * 3) * 6 + (7 + 5 + 4) + 8 * 8)", 12064),
("2 * 3 + ((8 * 8 * 8 + 4) * 7 * 7) * 5 * 8", 1011600),
("(6 * (7 + 3 + 8 * 5 + 2) + 2 * 4 + 5 * 5) * 2 * (8 + 3 + 3 + (6 + 4) * (9 * 3)) + 6 * 9", 129528774),
("8 * (7 * 6 * 8 * (3 * 6 + 4 + 6 + 2 + 2) * 6) + 8 * 8 * 6", 24772992),
("(7 * 5 * 5) * 7 * ((3 + 6 + 3 + 4 + 2 * 2) * 8 * (7 + 9 * 9 * 9))", 457228800),
("((5 + 7 * 9 * 2 + 7) + 7 + 5 * 6 + 3) * 6 * 2 * 3", 50868),
("5 + (5 * (9 + 9) + (7 * 7 * 4 + 2) * 3)", 869),
("((8 * 8 + 2 + 8 * 9) * 5) + (2 + (9 * 6 + 9 + 7) * 7)", 3834),
("3 + ((7 * 4 * 4 * 5) * (8 + 3 * 4 + 4 * 9) * 5 * 3 + 7) + 4 * 2 * 7 * (3 * 6 * 6)", 5486766768),
("((9 * 7 + 6 + 7 * 9 + 2) * 4 + 3 + 4 + 4 * 6) * 8 + (4 + (8 + 2 * 6 * 4 * 3)) + (5 + 2 * 6 * (4 * 5 + 4 + 2)) + 4 * (6 + 3 * (2 * 2 * 6 * 3) + 8 + 4 * 5)", 442398000),
("(5 * (7 + 9 + 5) + 6) * 5 * (6 + 7 * 9 + 6) * 9 * 5 + 9", 3071934),
("(4 * 4 * 6 * (5 * 8 + 3 + 8) + 5) + (9 * (3 * 9 + 3 * 9) + 5 + 8 + 2 * 3) * 3 * 2 + 8 * 5", 367120),
("7 + 5 * (3 + 8 + 6 * 9 + 5 * 8) * ((9 * 9 * 4 * 9 + 3) * (6 + 9 * 5) * 8 + 4 + 6)", 26565386880),
("7 + 8 * ((3 + 7 * 5) + 3 * 2 + 2 * 3 + 7) * 9 + 8 * 7", 312851),
("3 + 4 + (5 * (9 + 9 + 3 * 6 * 4 * 3) * 4) * (4 * (7 * 6 * 3 + 5 * 2 * 9) + (7 * 3 + 5))", 286076126),
("3 * (2 + 8 + 2) * 6 * (4 * 4)", 3456),
("2 + 2 + 3 * 5 + (3 + (6 + 2) + 9 + 7) * (3 * 3 + 5 + 8 + 7)", 1798),
("4 + 5 + 6 * 6 * 6 + (3 * 2 * 3 * (2 * 6 + 9) + 6 + (5 + 2 + 9 * 6))", 1020),
("((4 + 9) + 2) * 7 + 3 * 4", 432),
("2 + 6 * ((2 * 7 * 8 * 3 + 8 + 4) + 9 + 6 + 9 * 2 + 9) + 7 * 6", 36186),
("9 * 7 + ((2 + 5 * 2 * 7 * 8) + 6 * 2 + 3 + 4) * 5 * (5 + 2 * 7) * 8", 3234000),
("4 * (8 + 5 * 6 * 8) + 2 + 3 * 7", 17507),
("(2 * 2) * 9 * 6 * 3 + 9", 657),
("9 + 2 * (7 + 5 + 7 + (3 + 7 * 2 * 2 + 3) * 3)", 2046),
("(2 + 5 * 9) * (4 * (9 + 2 * 4 + 3 + 2) * 2 + 8 + (6 * 7 + 9 + 5)) * 3 + 8", 86192),
("(6 * (6 + 7 * 7) + 7 * 6 * 7) + ((7 * 6 * 4) * 6 * (8 + 8 + 5 * 7 * 8 + 8) + (4 + 5 * 5 + 5 + 3 * 9) + 8)", 1217183),
("9 * (6 * 9 * (8 * 2 * 6) + 4 * 4)", 186768),
("(5 + 5 * 5 + 8) * 6 + 5 * 2", 706),
("4 + ((8 + 7) + 5) + 6", 30),
("9 + 6 * (7 + 4 + (9 + 4 + 2 + 8)) + (8 * 5)", 550),
("(3 * 8 + (6 + 9 * 8 + 6)) + 3 + 4 * 8 + 5 * 3", 3783),
("3 + 6 * (7 * 8 * 6 * (5 + 8 + 6) + 5 * 4) * ((8 + 9 + 6 * 8) * 9 + 6 * 6 + 2 * 2) * 4", 18352479168),
("7 * (6 * (5 * 5 + 9 * 7 + 5 + 9) + 8 + 9 + 2 * (6 * 7 * 7)) * 3", 9452394),
("4 + 2 * (5 * (3 * 8 * 3) * (5 + 9 + 8 * 9) + 9 + 7) * 7 + 5", 2994437),
("6 * 5 * (5 * 3 * 9 * 4 * 2) * 9 + 7 * 4", 1166428),
("3 * (4 * (7 + 2) + 4 + 6 + (3 + 6 + 6 * 5)) + 5", 368),
("7 * 7 * (5 + 9 + 9) * (8 * 6 * 5 + 4 * 2)", 549976),
("6 + (9 * 4) * ((4 * 2 * 4 * 4 * 6 * 7) + 9 + (2 * 9 + 9 + 9)) + 2 + 4 + 4", 227692),
("9 * (6 * 5 + 9 * 2 * 4) + 7 + 8 * 8 + 9", 22593),
("(6 + 7 + 8 * 2 * 4) * 5 + 2 + 2 * 5 + 6", 4226),
("8 * 7 * (9 * 2 * 4 + 3 + 4 * 4) + (8 * 8 + 9 + 6) + ((7 * 7 + 2 * 9) * (2 * 8 + 2) * 6 * 6) + 8", 315215),
];
// Find shortest failures first, that should make it easier to understand.
ts.sort_by(|l, r| l.0.len().cmp(&r.0.len()));
for (input, want) in ts {
let got = parse_part1(&lex(input));
assert_eq!(got, want, "got {}, want {} for {}", got, want, input);
}
}
#[test] #[test]
fn comprehensive() { fn comprehensive_part2() {
let mut ts = vec![ let mut ts = vec![
("(7 * (3 + 8 + 8 + 7) + (6 + 8 * 2 + 5 + 2 * 6) * (5 + 2) * 9) + ((7 * 4 + 8) * 6 * 8 + 9) * 7 * 2 * 2", 9896040), ("(7 * (3 + 8 + 8 + 7) + (6 + 8 * 2 + 5 + 2 * 6) * (5 + 2) * 9) + ((7 * 4 + 8) * 6 * 8 + 9) * 7 * 2 * 2", 9896040),
("6 * ((9 + 4) * (6 * 7 + 5 + 8 * 2))", 18720), ("6 * ((9 + 4) * (6 * 7 + 5 + 8 * 2))", 18720),
@ -564,8 +1057,7 @@ mod tests {
// Find shortest failures first, that should make it easier to understand. // Find shortest failures first, that should make it easier to understand.
ts.sort_by(|l, r| l.0.len().cmp(&r.0.len())); ts.sort_by(|l, r| l.0.len().cmp(&r.0.len()));
for (input, want) in ts { for (input, want) in ts {
let got = parse(&lex(input)); let got = parse_part2(&lex(input));
eprintln!("got {}, want {} for {}", got, want, input);
assert_eq!(got, want, "got {}, want {} for {}", got, want, input); assert_eq!(got, want, "got {}, want {} for {}", got, want, input);
} }
} }

185
2020/src/day23.rs Normal file
View File

@ -0,0 +1,185 @@
//! --- Day 23: Crab Cups ---
//! The small crab challenges you to a game! The crab is going to mix up some cups, and you have to predict where they'll end up.
//!
//! The cups will be arranged in a circle and labeled clockwise (your puzzle input). For example, if your labeling were 32415, there would be five cups in the circle; going clockwise around the circle from the first cup, the cups would be labeled 3, 2, 4, 1, 5, and then back to 3 again.
//!
//! Before the crab starts, it will designate the first cup in your list as the current cup. The crab is then going to do 100 moves.
//!
//! Each move, the crab does the following actions:
//!
//! The crab picks up the three cups that are immediately clockwise of the current cup. They are removed from the circle; cup spacing is adjusted as necessary to maintain the circle.
//! The crab selects a destination cup: the cup with a label equal to the current cup's label minus one. If this would select one of the cups that was just picked up, the crab will keep subtracting one until it finds a cup that wasn't just picked up. If at any point in this process the value goes below the lowest value on any cup's label, it wraps around to the highest value on any cup's label instead.
//! The crab places the cups it just picked up so that they are immediately clockwise of the destination cup. They keep the same order as when they were picked up.
//! The crab selects a new current cup: the cup which is immediately clockwise of the current cup.
//! For example, suppose your cup labeling were 389125467. If the crab were to do merely 10 moves, the following changes would occur:
//!
//! -- move 1 --
//! cups: (3) 8 9 1 2 5 4 6 7
//! pick up: 8, 9, 1
//! destination: 2
//!
//! -- move 2 --
//! cups: 3 (2) 8 9 1 5 4 6 7
//! pick up: 8, 9, 1
//! destination: 7
//!
//! -- move 3 --
//! cups: 3 2 (5) 4 6 7 8 9 1
//! pick up: 4, 6, 7
//! destination: 3
//!
//! -- move 4 --
//! cups: 7 2 5 (8) 9 1 3 4 6
//! pick up: 9, 1, 3
//! destination: 7
//!
//! -- move 5 --
//! cups: 3 2 5 8 (4) 6 7 9 1
//! pick up: 6, 7, 9
//! destination: 3
//!
//! -- move 6 --
//! cups: 9 2 5 8 4 (1) 3 6 7
//! pick up: 3, 6, 7
//! destination: 9
//!
//! -- move 7 --
//! cups: 7 2 5 8 4 1 (9) 3 6
//! pick up: 3, 6, 7
//! destination: 8
//!
//! -- move 8 --
//! cups: 8 3 6 7 4 1 9 (2) 5
//! pick up: 5, 8, 3
//! destination: 1
//!
//! -- move 9 --
//! cups: 7 4 1 5 8 3 9 2 (6)
//! pick up: 7, 4, 1
//! destination: 5
//!
//! -- move 10 --
//! cups: (5) 7 4 1 8 3 9 2 6
//! pick up: 7, 4, 1
//! destination: 3
//!
//! -- final --
//! cups: 5 (8) 3 7 4 1 9 2 6
//! In the above example, the cups' values are the labels as they appear moving clockwise around the circle; the current cup is marked with ( ).
//!
//! After the crab is done, what order will the cups be in? Starting after the cup labeled 1, collect the other cups' labels clockwise into a single string with no extra characters; each number except 1 should appear exactly once. In the above example, after 10 moves, the cups clockwise from 1 are labeled 9, 2, 6, 5, and so on, producing 92658374. If the crab were to complete all 100 moves, the order after cup 1 would be 67384529.
//!
//! Using your labeling, simulate 100 moves. What are the labels on the cups after cup 1?
use std::fmt;
use aoc_runner_derive::aoc;
struct Hand {
cups: Vec<usize>,
cur: usize,
min: usize,
max: usize,
}
impl fmt::Display for Hand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, cup) in self.cups.iter().enumerate() {
if i == self.cur {
write!(f, "({}) ", cup)?;
} else {
write!(f, "{} ", cup)?;
};
}
Ok(())
}
}
impl Hand {
fn new(s: &str) -> Hand {
let cups: Vec<_> = s.bytes().map(|s| (s - b'0') as usize).collect();
let min = *cups.iter().min().unwrap();
let max = *cups.iter().max().unwrap();
Hand {
cups,
cur: 0,
min,
max,
}
}
fn play(&mut self, rounds: usize) -> String {
(0..rounds).for_each(|i| {
println!("-- move {} --", i + 1);
self.step();
});
self.answer()
}
fn step(&mut self) {
println!("{}", self);
let cur = self.cups[self.cur];
let mut pickups = Vec::new();
let mut destination = self.cups[self.cur] - 1;
let mut rm_idx = (self.cur + 1) % self.cups.len();
(0..3).for_each(|_| {
pickups.push(self.cups.remove(rm_idx));
if rm_idx >= self.cups.len() {
rm_idx -= self.cups.len();
}
});
let cur = self.cups.iter().position(|i| i == &cur).unwrap();
let next = self.cups[(cur + 1) % self.cups.len()];
while pickups.contains(&destination) {
destination -= 1;
}
if destination < self.min {
destination = self.max;
while pickups.contains(&destination) {
destination -= 1;
}
}
//dbg!(&pickups, &self.cups, destination);
let idx = self.cups.iter().position(|i| i == &destination).unwrap();
println!("pick up: {:?}", pickups);
println!("destination: {}({})", destination, idx);
println!("next destination: {}", next);
pickups
.into_iter()
.rev()
.for_each(|v| self.cups.insert(idx + 1, v));
self.cur = self.cups.iter().position(|i| i == &next).unwrap();
}
fn answer(&self) -> String {
let idx = self.cups.iter().position(|i| i == &1).unwrap();
let s = self.cups[idx + 1..]
.iter()
.fold("".to_string(), |acc, c| format!("{}{}", acc, c));
self.cups[..idx]
.iter()
.fold(s, |acc, c| format!("{}{}", acc, c))
}
}
#[aoc(day23, part1)]
fn solution1(input: &str) -> String {
let mut hand = Hand::new(input);
hand.play(100)
}
#[cfg(test)]
mod tests {
use super::*;
const INPUT: &'static str = "389125467";
#[test]
fn part1_10step() {
let mut hand = Hand::new(INPUT);
assert_eq!(hand.play(10), "92658374");
}
//#[test]
fn part1() {
assert_eq!(solution1(INPUT), "67384529");
}
}

View File

@ -13,6 +13,7 @@ pub mod day2;
//pub mod day20; //pub mod day20;
pub mod day21; pub mod day21;
pub mod day22; pub mod day22;
pub mod day23;
pub mod day3; pub mod day3;
pub mod day4; pub mod day4;
pub mod day5; pub mod day5;