fn parse(input: &str) -> Vec<(u64, Vec<u64>)> {
input
- .split_terminator("\n")
+ .lines()
.map(|l| {
let [lhs, rhs] = l.split(":").collect::<Vec<_>>()[..] else {
panic!()
.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,
})
}
"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)
}
)
}
+ #[rustfmt::skip]
#[test]
fn test_opstring_with_cat() {
use Op::*;
assert_eq!(
OpString::new_with_cat(2).collect::<Vec<_>>(),
[
- [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!(