diff --git a/src/euler51/src/main.rs b/src/euler51/src/main.rs index f52d0a5..62fecd8 100644 --- a/src/euler51/src/main.rs +++ b/src/euler51/src/main.rs @@ -12,12 +12,6 @@ type Int = u64; // const LENGTH: usize = 2; // const MIN_LENGTH: usize = 1; -// OWN TEST -// const MIN_VALUE: Int = 100; -// const MAX_VALUE: Int = 999; -// const LENGTH: usize = 3; -// const MIN_LENGTH: usize = 2; - // EXAMPLE 2 // const MIN_VALUE: Int = 10_000; // const MAX_VALUE: Int = 99_999; @@ -49,24 +43,26 @@ fn main() { } } if primes_matching_pattern.len() > 2 { - best_this_combination.push(primes_matching_pattern) + best_this_combination.push(primes_matching_pattern); } } if !best_this_combination.is_empty() { - best_of_all_combinations.push(get_longest(best_this_combination, false)); + best_of_all_combinations.push(get_longest(best_this_combination)); } } - println!("{:#?}", get_longest(best_of_all_combinations, false)); + println!("{:#?}", get_longest(best_of_all_combinations)); } fn apply_transformation(prime_as_digits: &[u8], new_digit: u8, pattern: &[bool]) -> Int { debug_assert_eq!(prime_as_digits.len(), pattern.len()); let mut multiplier = 1; let result = prime_as_digits + // apply the pattern to prime_as_digits .iter() .zip(pattern) .map(|(old_digit, replace)| if *replace { new_digit } else { *old_digit }) .map(|d| d as Int) + // calculate Int .rev() .reduce(|accum, item| { multiplier *= 10; @@ -77,13 +73,11 @@ fn apply_transformation(prime_as_digits: &[u8], new_digit: u8, pattern: &[bool]) result } -fn get_longest(mut vec: Vec>, print: bool) -> Vec { - if vec.is_empty() { - panic!("cannot give the longest element of an empty vector") - } - if print { - println!("{:#?}", vec); - } +fn get_longest(mut vec: Vec>) -> Vec { + assert!( + !vec.is_empty(), + "cannot get the longest element of an empty vector" + ); vec.sort_by_key(|c| c.len()); let length = vec.last().unwrap().len(); vec.into_iter().find(|v| v.len() == length).unwrap() @@ -93,10 +87,6 @@ trait ToDigits { fn to_digits(&self) -> Vec; } -trait ToNum { - fn to_num(&self) -> Int; -} - impl ToDigits for Int { fn to_digits(&self) -> Vec { self.to_string() @@ -106,27 +96,9 @@ impl ToDigits for Int { } } -impl ToNum for Vec { - fn to_num(&self) -> Int { - let mut result = 0; - let mut position = 1; - for digit in self.iter().rev() { - result += *digit as Int * position; - position *= 10; - } - result - } -} - #[cfg(test)] mod test { - use crate::{apply_transformation, ToDigits, ToNum}; - - #[test] - fn test_digits_to_num() { - let digits = vec![5, 7, 3, 8, 1]; - assert_eq!(digits.to_num(), 57381); - } + use crate::{apply_transformation, ToDigits}; #[test] fn test_num_to_digits() {