From f5d44b9672dec2fc157c17e73d25e1dff29267bc Mon Sep 17 00:00:00 2001 From: LeMoonStar Date: Sun, 8 Dec 2024 00:33:53 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Day=207:=20Performance=20b?= =?UTF-8?q?oost=20on=20part=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/days/d07.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/days/d07.rs b/src/days/d07.rs index 21257b8..34b165e 100644 --- a/src/days/d07.rs +++ b/src/days/d07.rs @@ -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, } } }