Skip to content

Commit

Permalink
Rewrite 07 using then_some()
Browse files Browse the repository at this point in the history
  • Loading branch information
Nekuskus committed Dec 7, 2024
1 parent 3173c18 commit 2ebcb21
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions src/07.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ fn process_ops(ops: Vec<&Ops>, operands: &Vec<i64>) -> i64 {
operands[1..]
.iter()
.enumerate()
.fold(operands[0], |acc, (idx, &num)| match ops[idx] {
Ops::Add => acc + num,
Ops::Multiply => acc * num,
Ops::Concat => acc * 10i64.pow(num.ilog10() + 1) + num,
.fold(operands[0], |acc, (idx, &rhs)| match ops[idx] {
Ops::Add => acc + rhs,
Ops::Multiply => acc * rhs,
Ops::Concat => acc * 10i64.pow(rhs.ilog10() + 1) + rhs,
})
}

Expand All @@ -108,14 +108,10 @@ fn part1(lines: &Vec<String>) -> i64 {
.map(|s| s.parse::<i64>().unwrap())
.collect_vec();

if itertools::repeat_n(operations.iter(), operands.len() - 1) // permutation with replacements
itertools::repeat_n(operations.iter(), operands.len() - 1) // permutation with replacements
.multi_cartesian_product()
.any(|ops| process_ops(ops, &operands) == lhs)
{
Some(lhs)
} else {
None
}
.then_some(lhs)
})
.sum()
}
Expand All @@ -136,14 +132,10 @@ fn part2(lines: &Vec<String>) -> i64 {
.map(|s| s.parse::<i64>().unwrap())
.collect_vec();

if itertools::repeat_n(operations.iter(), operands.len() - 1) // permutation with replacements
itertools::repeat_n(operations.iter(), operands.len() - 1) // permutation with replacements
.multi_cartesian_product()
.any(|ops| process_ops(ops, &operands) == lhs)
{
Some(lhs)
} else {
None
}
.then_some(lhs)
})
.sum()
}

0 comments on commit 2ebcb21

Please sign in to comment.