diff --git a/src/euler52/Cargo.toml b/src/euler52/Cargo.toml new file mode 100644 index 0000000..5a43bb0 --- /dev/null +++ b/src/euler52/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "euler52" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/euler52/src/main.rs b/src/euler52/src/main.rs new file mode 100644 index 0000000..dfbca50 --- /dev/null +++ b/src/euler52/src/main.rs @@ -0,0 +1,63 @@ +use std::collections::HashMap; + +const MULTIPLES_TO_CHECK: [u64; 5] = [2, 3, 4, 5, 6]; + +fn main() { + let mut i = 10; + + 'outer: loop { + i += 1; + let hm1 = get_amount_of_digits(i); + for factor in MULTIPLES_TO_CHECK { + let double = i * factor; + let hm2 = get_amount_of_digits(double); + if hm1 != hm2 { + continue 'outer; + } + } + break; + } + println!("{i}"); +} + +fn get_amount_of_digits(i: u64) -> HashMap { + let mut result = get_empty_hashmap(); + i.to_string() + .chars() + .map(|c| c.to_digit(10).unwrap() as u64) + .for_each(|d| { + result.insert(d, result[&d] + 1); + }); + result +} + +fn get_empty_hashmap() -> HashMap { + let mut hm = HashMap::new(); + for i in 0..10 { + hm.insert(i, 0); + } + hm +} + +#[cfg(test)] +mod test { + use crate::{get_amount_of_digits, get_empty_hashmap}; + + #[test] + fn test_get_amount_of_digits() { + let mut correct = get_empty_hashmap(); + correct.insert(2, 1); + correct.insert(5, 2); + correct.insert(0, 1); + assert_eq!(correct, get_amount_of_digits(5250)); + } + + #[test] + fn test_get_empty_hashmap() { + let hm = get_empty_hashmap(); + assert_eq!(hm.len(), 10); + for (_, v) in hm { + assert_eq!(v, 0); + } + } +}