refactor 51
This commit is contained in:
@@ -8,3 +8,6 @@ edition = "2021"
|
||||
[dependencies]
|
||||
primes = {version = "*", path = "../../lib/primes"}
|
||||
combination = {version = "*", path = "../../lib/combination"}
|
||||
|
||||
[profile.release]
|
||||
debug = true
|
||||
|
||||
+31
-7
@@ -43,12 +43,7 @@ fn main() {
|
||||
let mut primes_matching_pattern = vec![];
|
||||
let digits = first.to_digits();
|
||||
for digit in DIGITS {
|
||||
let could_be_prime = digits
|
||||
.iter()
|
||||
.zip(pattern.clone())
|
||||
.map(|(d, b)| if b { digit } else { *d })
|
||||
.collect::<Vec<u8>>()
|
||||
.to_num();
|
||||
let could_be_prime = apply_transformation(&digits, digit, &pattern);
|
||||
if primes_to_check.contains(&could_be_prime) || could_be_prime == first {
|
||||
primes_matching_pattern.push(could_be_prime);
|
||||
}
|
||||
@@ -64,6 +59,24 @@ fn main() {
|
||||
println!("{:#?}", get_longest(best_of_all_combinations, false));
|
||||
}
|
||||
|
||||
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
|
||||
.iter()
|
||||
.zip(pattern)
|
||||
.map(|(old_digit, replace)| if *replace { new_digit } else { *old_digit })
|
||||
.map(|d| d as Int)
|
||||
.rev()
|
||||
.reduce(|accum, item| {
|
||||
multiplier *= 10;
|
||||
accum + multiplier * item
|
||||
})
|
||||
.unwrap();
|
||||
debug_assert_eq!(multiplier, 100_000);
|
||||
result
|
||||
}
|
||||
|
||||
fn get_longest(mut vec: Vec<Vec<Int>>, print: bool) -> Vec<Int> {
|
||||
if vec.is_empty() {
|
||||
panic!("cannot give the longest element of an empty vector")
|
||||
@@ -107,7 +120,7 @@ impl ToNum for Vec<u8> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{ToDigits, ToNum};
|
||||
use crate::{apply_transformation, ToDigits, ToNum};
|
||||
|
||||
#[test]
|
||||
fn test_digits_to_num() {
|
||||
@@ -120,4 +133,15 @@ mod test {
|
||||
let num = 83371;
|
||||
assert_eq!(num.to_digits(), vec![8, 3, 3, 7, 1]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_apply_transformation() {
|
||||
let prime_as_digits = [5, 7, 3, 8, 2, 1];
|
||||
let new_digit = 0;
|
||||
let pattern = [false, true, true, true, false, false];
|
||||
assert_eq!(
|
||||
apply_transformation(&prime_as_digits, new_digit, &pattern),
|
||||
500_021
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user