From 3bdef3e1e178b4cc339b2650c66229e099f7b9fa Mon Sep 17 00:00:00 2001 From: Jack Kinsey Date: Sat, 7 Dec 2024 17:38:05 -0500 Subject: [PATCH] Tidy up day 7 a little --- src/day07.rs | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/src/day07.rs b/src/day07.rs index 63cd0d4..d8e3d9d 100644 --- a/src/day07.rs +++ b/src/day07.rs @@ -4,7 +4,7 @@ fn input() -> &'static str { fn parse(input: &str) -> Vec<(u64, Vec)> { input - .split_terminator("\n") + .lines() .map(|l| { let [lhs, rhs] = l.split(":").collect::>()[..] else { panic!() @@ -88,7 +88,7 @@ fn op_eval(num: &[u64], op: &[Op]) -> u64 { .fold(num[0], |a, (b, x)| match x { Op::Add => a + b, Op::Mul => a * b, - Op::Cat => a * 10u64.pow(b.ilog10() + 1) + b, + Op::Cat => a * b.next_multiple_of(10) + b, }) } @@ -134,26 +134,34 @@ mod test { "292: 11 6 16 20\n", ); + #[rustfmt::skip] #[test] fn test_parse() { assert_eq!( parse(INPUT_STR), [ - (190, vec![10, 19]), - (3267, vec![81, 40, 27]), - (83, vec![17, 5]), - (156, vec![15, 6]), - (7290, vec![6, 8, 6, 15]), + (190, vec![10, 19]), + (3267, vec![81, 40, 27]), + (83, vec![17, 5]), + (156, vec![15, 6]), + (7290, vec![6, 8, 6, 15]), (161011, vec![16, 10, 13]), - (192, vec![17, 8, 14]), - (21037, vec![9, 7, 18, 13]), - (292, vec![11, 6, 16, 20]) + (192, vec![17, 8, 14]), + (21037, vec![9, 7, 18, 13]), + (292, vec![11, 6, 16, 20]) ] ) } #[test] - fn test_op_eval() { + fn test_op_eval_ops() { + assert_eq!(op_eval(&[1, 1], &[Op::Add]), 2); + assert_eq!(op_eval(&[1, 1], &[Op::Mul]), 1); + assert_eq!(op_eval(&[1, 1], &[Op::Cat]), 11); + } + + #[test] + fn test_op_eval_fold() { assert_eq!(op_eval(&[1, 2, 3], &[Op::Add, Op::Mul]), 9) } @@ -166,32 +174,20 @@ mod test { ) } + #[rustfmt::skip] #[test] fn test_opstring_with_cat() { use Op::*; assert_eq!( OpString::new_with_cat(2).collect::>(), [ - [Add, Add], - [Add, Mul], - [Add, Cat], - [Mul, Add], - [Mul, Mul], - [Mul, Cat], - [Cat, Add], - [Cat, Mul], - [Cat, Cat], + [Add, Add], [Add, Mul], [Add, Cat], + [Mul, Add], [Mul, Mul], [Mul, Cat], + [Cat, Add], [Cat, Mul], [Cat, Cat], ] ) } - #[test] - fn test_check_op_eval() { - assert_eq!(op_eval(&[1, 1], &[Op::Add]), 2); - assert_eq!(op_eval(&[1, 1], &[Op::Mul]), 1); - assert_eq!(op_eval(&[1, 1], &[Op::Cat]), 11); - } - #[test] fn test_check_ops() { assert_eq!( -- 2.38.5