pub fn blink(mut stones: Stones, i: u32) -> Stones {
for _ in 0..i {
- let mut next_stones = Stones::default();
- for (stone, n) in stones {
+ for (stone, n) in stones.drain().collect::<Vec<_>>() {
if stone == 0 {
- add_or_set(&mut next_stones, 1, n);
+ add_or_set(&mut stones, 1, n);
} else {
- let nb_digits = utils::nb_of_digits(stone);
+ let nb_digits = utils::nb_of_digits_u64(stone);
if nb_digits % 2 == 0 {
let base = 10u64.pow(nb_digits / 2);
- let left = stone / base;
- let right = stone - left * base;
- add_or_set(&mut next_stones, left, n);
- add_or_set(&mut next_stones, right, n);
+ add_or_set(&mut stones, stone / base, n);
+ add_or_set(&mut stones, stone % base, n);
} else {
- add_or_set(&mut next_stones, stone * 2024, n);
+ add_or_set(&mut stones, stone * 2024, n);
}
}
}
- stones = next_stones;
}
stones
}
-use std::{cmp, fmt::Debug, str::FromStr};
-
-use num;
+use std::{fmt::Debug, str::FromStr};
pub fn split_to_vec<T>(str: &str, pat: &str) -> Vec<T>
where
str.split(pat).map(|v| v.parse::<T>().unwrap()).collect()
}
-pub fn nb_of_digits<T>(mut v: T) -> u32
-where
- T: cmp::PartialOrd<T> + std::ops::DivAssign<T> + num::FromPrimitive + Copy,
-{
- let ten = T::from_u32(10).unwrap();
- let mut n = 1;
- while v >= ten {
- v /= ten;
- n += 1;
- }
- n
+pub fn nb_of_digits_u64(v: u64) -> u32 {
+ v.ilog10() + 1
+}
+
+pub fn nb_of_digits_i64(v: i64) -> u32 {
+ v.ilog10() + 1
}