mirror of
https://github.com/LeMoonStar/AoC24.git
synced 2025-07-08 00:10:00 +02:00
⚡️ Day 7: Performance boost on part 2
This commit is contained in:
parent
28e2a563c0
commit
f5d44b9672
1 changed files with 15 additions and 1 deletions
|
@ -10,11 +10,25 @@ enum Operator {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
fn calculate(&self, a: u64, b: u64) -> u64 {
|
||||||
match self {
|
match self {
|
||||||
Operator::Multiply => a * b,
|
Operator::Multiply => a * b,
|
||||||
Operator::Add => a + b,
|
Operator::Add => a + b,
|
||||||
Operator::Concat => format!("{}{}", a, b).parse().unwrap(),
|
Operator::Concat => a * 10_u64.pow(Self::num_digits(b)) + b,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue