️ Day 7: Performance boost on part 2

This commit is contained in:
LeMoonStar 2024-12-08 00:33:53 +01:00
parent 28e2a563c0
commit f5d44b9672

View file

@ -10,11 +10,25 @@ enum Operator {
}
impl Operator {
#[inline(always)]
fn num_digits(mut n: u64) -> u32 {
if n == 0 {
return 1;
}
let mut count = 0;
while n > 0 {
n /= 10;
count += 1;
}
count
}
#[inline(always)]
fn calculate(&self, a: u64, b: u64) -> u64 {
match self {
Operator::Multiply => a * b,
Operator::Add => a + b,
Operator::Concat => format!("{}{}", a, b).parse().unwrap(),
Operator::Concat => a * 10_u64.pow(Self::num_digits(b)) + b,
}
}
}