finished 49

This commit is contained in:
Dr. Matthias Ratajczak
2022-08-10 19:18:19 +02:00
parent 31dba279b3
commit b6b05b7df3
+40 -7
View File
@@ -5,6 +5,7 @@ const MIN_VALUE: Int = 2;
const MAX_VALUE: Int = 10_000;
const SIZE: usize = (MAX_VALUE - MIN_VALUE) as usize;
type Array = [bool; SIZE];
type Solution = [Int; 3];
fn main() {
println!("Getting prime numbers up to {}", MAX_VALUE);
@@ -51,13 +52,13 @@ impl Primes {
}
}
fn find_using_iter(primes: &Primes) -> [Int; 3] {
fn find_using_iter(primes: &Primes) -> Solution {
for p1 in primes.vector.iter() {
let p2 = p1 + 3330;
let p3 = p2 + 3330;
if primes.set.contains(&p2) && primes.set.contains(&p3) {
let candidate = [*p1, p2, p3];
if satisfies_checks(&candidate) {
if satisfies_checks(&candidate) && candidate[0] != 1487 {
return candidate;
}
}
@@ -65,13 +66,12 @@ fn find_using_iter(primes: &Primes) -> [Int; 3] {
panic!()
}
fn satisfies_checks(cand: &[Int; 3]) -> bool {
cand.iter()
.map(|x| get_amount_of_digits(x))
.filter(|x, y| x == y)
fn satisfies_checks(candidates: &Solution) -> bool {
let vec: Vec<_> = candidates.iter().map(get_amount_of_digits).collect();
vec[0] == vec[1] && vec[0] == vec[2]
}
fn get_amount_of_digits(x: Int) -> HashMap<i32, usize> {
fn get_amount_of_digits(x: &Int) -> HashMap<i32, usize> {
let mut map = get_new_map();
x.to_string()
.chars()
@@ -85,3 +85,36 @@ fn get_amount_of_digits(x: Int) -> HashMap<i32, usize> {
fn get_new_map() -> HashMap<i32, usize> {
(0..=9).into_iter().zip([0; 10]).collect()
}
#[cfg(test)]
mod tests {
use crate::{get_amount_of_digits, get_new_map, satisfies_checks, Solution};
#[test]
fn test_get_amount_of_digits() {
let x = 4731;
let hs = get_amount_of_digits(&x);
let mut reference = get_new_map();
reference.insert(4, 1);
reference.insert(7, 1);
reference.insert(3, 1);
reference.insert(1, 1);
assert_eq!(hs, reference);
let x = 4777;
let hs = get_amount_of_digits(&x);
let mut reference = get_new_map();
reference.insert(4, 1);
reference.insert(7, 3);
assert_eq!(hs, reference);
}
#[test]
fn test_satisfies_checks() {
let input: Solution = [1234, 3241, 4321];
assert!(satisfies_checks(&input));
let input: Solution = [1254, 3241, 4321];
assert!(!satisfies_checks(&input));
}
}