clean up 51

This commit is contained in:
timeshifter
2022-08-14 22:36:39 +02:00
parent 243754f1fa
commit 332da67bef
+11 -39
View File
@@ -12,12 +12,6 @@ type Int = u64;
// const LENGTH: usize = 2; // const LENGTH: usize = 2;
// const MIN_LENGTH: usize = 1; // 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 // EXAMPLE 2
// const MIN_VALUE: Int = 10_000; // const MIN_VALUE: Int = 10_000;
// const MAX_VALUE: Int = 99_999; // const MAX_VALUE: Int = 99_999;
@@ -49,24 +43,26 @@ fn main() {
} }
} }
if primes_matching_pattern.len() > 2 { 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() { 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 { fn apply_transformation(prime_as_digits: &[u8], new_digit: u8, pattern: &[bool]) -> Int {
debug_assert_eq!(prime_as_digits.len(), pattern.len()); debug_assert_eq!(prime_as_digits.len(), pattern.len());
let mut multiplier = 1; let mut multiplier = 1;
let result = prime_as_digits let result = prime_as_digits
// apply the pattern to prime_as_digits
.iter() .iter()
.zip(pattern) .zip(pattern)
.map(|(old_digit, replace)| if *replace { new_digit } else { *old_digit }) .map(|(old_digit, replace)| if *replace { new_digit } else { *old_digit })
.map(|d| d as Int) .map(|d| d as Int)
// calculate Int
.rev() .rev()
.reduce(|accum, item| { .reduce(|accum, item| {
multiplier *= 10; multiplier *= 10;
@@ -77,13 +73,11 @@ fn apply_transformation(prime_as_digits: &[u8], new_digit: u8, pattern: &[bool])
result result
} }
fn get_longest(mut vec: Vec<Vec<Int>>, print: bool) -> Vec<Int> { fn get_longest(mut vec: Vec<Vec<Int>>) -> Vec<Int> {
if vec.is_empty() { assert!(
panic!("cannot give the longest element of an empty vector") !vec.is_empty(),
} "cannot get the longest element of an empty vector"
if print { );
println!("{:#?}", vec);
}
vec.sort_by_key(|c| c.len()); vec.sort_by_key(|c| c.len());
let length = vec.last().unwrap().len(); let length = vec.last().unwrap().len();
vec.into_iter().find(|v| v.len() == length).unwrap() vec.into_iter().find(|v| v.len() == length).unwrap()
@@ -93,10 +87,6 @@ trait ToDigits {
fn to_digits(&self) -> Vec<u8>; fn to_digits(&self) -> Vec<u8>;
} }
trait ToNum {
fn to_num(&self) -> Int;
}
impl ToDigits for Int { impl ToDigits for Int {
fn to_digits(&self) -> Vec<u8> { fn to_digits(&self) -> Vec<u8> {
self.to_string() self.to_string()
@@ -106,27 +96,9 @@ impl ToDigits for Int {
} }
} }
impl ToNum for Vec<u8> {
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)] #[cfg(test)]
mod test { mod test {
use crate::{apply_transformation, ToDigits, ToNum}; use crate::{apply_transformation, ToDigits};
#[test]
fn test_digits_to_num() {
let digits = vec![5, 7, 3, 8, 1];
assert_eq!(digits.to_num(), 57381);
}
#[test] #[test]
fn test_num_to_digits() { fn test_num_to_digits() {