day3: add clever solution
This commit is contained in:
@@ -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))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user