refactor 51
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
flamegraph.svg
|
||||||
|
perf.data
|
||||||
|
perf.data.old
|
||||||
# Created by https://www.toptal.com/developers/gitignore/api/rust,intellij,python
|
# Created by https://www.toptal.com/developers/gitignore/api/rust,intellij,python
|
||||||
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,intellij,python
|
# Edit at https://www.toptal.com/developers/gitignore?templates=rust,intellij,python
|
||||||
|
|
||||||
|
|||||||
@@ -8,3 +8,6 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
primes = {version = "*", path = "../../lib/primes"}
|
primes = {version = "*", path = "../../lib/primes"}
|
||||||
combination = {version = "*", path = "../../lib/combination"}
|
combination = {version = "*", path = "../../lib/combination"}
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
|
|||||||
+31
-7
@@ -43,12 +43,7 @@ fn main() {
|
|||||||
let mut primes_matching_pattern = vec![];
|
let mut primes_matching_pattern = vec![];
|
||||||
let digits = first.to_digits();
|
let digits = first.to_digits();
|
||||||
for digit in DIGITS {
|
for digit in DIGITS {
|
||||||
let could_be_prime = digits
|
let could_be_prime = apply_transformation(&digits, digit, &pattern);
|
||||||
.iter()
|
|
||||||
.zip(pattern.clone())
|
|
||||||
.map(|(d, b)| if b { digit } else { *d })
|
|
||||||
.collect::<Vec<u8>>()
|
|
||||||
.to_num();
|
|
||||||
if primes_to_check.contains(&could_be_prime) || could_be_prime == first {
|
if primes_to_check.contains(&could_be_prime) || could_be_prime == first {
|
||||||
primes_matching_pattern.push(could_be_prime);
|
primes_matching_pattern.push(could_be_prime);
|
||||||
}
|
}
|
||||||
@@ -64,6 +59,24 @@ fn main() {
|
|||||||
println!("{:#?}", get_longest(best_of_all_combinations, false));
|
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> {
|
fn get_longest(mut vec: Vec<Vec<Int>>, print: bool) -> Vec<Int> {
|
||||||
if vec.is_empty() {
|
if vec.is_empty() {
|
||||||
panic!("cannot give the longest element of an empty vector")
|
panic!("cannot give the longest element of an empty vector")
|
||||||
@@ -107,7 +120,7 @@ impl ToNum for Vec<u8> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::{ToDigits, ToNum};
|
use crate::{apply_transformation, ToDigits, ToNum};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_digits_to_num() {
|
fn test_digits_to_num() {
|
||||||
@@ -120,4 +133,15 @@ mod test {
|
|||||||
let num = 83371;
|
let num = 83371;
|
||||||
assert_eq!(num.to_digits(), vec![8, 3, 3, 7, 1]);
|
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