day3: add clever solution

This commit is contained in:
2025-12-03 00:46:44 -08:00
parent f89bbf2bff
commit 6888466105
2 changed files with 56 additions and 17 deletions

View File

@@ -2,6 +2,19 @@ use num_traits::Signed;
use std::fmt::Display;
use std::ops::{Add, AddAssign};
const POW10MAX: usize = u64::MAX.ilog10() as usize;
pub const POW10: [u64; POW10MAX] = pow10_lut();
const fn pow10_lut<const N: usize>() -> [u64; N] {
let mut res = [0; N];
let mut i = 0;
while i < N {
res[i] = 10u64.pow(i as u32);
i += 1;
}
res
}
/// Wrapped signed integer with custom upper bound with wrapping of 0s to the upper bound
#[derive(Eq, Clone, Copy)]
pub struct CustomWrapped<T: Signed + Copy> {
@@ -91,8 +104,9 @@ mod tests {
use super::*;
#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
fn test_pow10() {
for i in 0..POW10MAX {
assert_eq!(POW10[i], 10u64.pow(i as u32))
}
}
}