From bbc4026698d96ef3e8ad47a60c9bfe9a9e2ef444 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Thu, 29 Aug 2024 20:25:18 -0500 Subject: [PATCH 1/3] add initial attempt at 111 --- _data/answers.tsv | 1 + rust/src/include/problems.rs | 2 ++ rust/src/lib.rs | 1 + rust/src/main.rs | 2 ++ rust/src/p0111.rs | 22 ++++++++++++++++++++++ 5 files changed, 28 insertions(+) create mode 100644 rust/src/p0111.rs diff --git a/_data/answers.tsv b/_data/answers.tsv index 0ad4a34c..d6cf6491 100644 --- a/_data/answers.tsv +++ b/_data/answers.tsv @@ -70,6 +70,7 @@ ID type size answer 92 int 32 8581146 97 int 64 8739992577 99 int 16 709 +111 uint 64 0 118 uint 16 44680 123 int 16 21035 134 int 64 18613426663617118 diff --git a/rust/src/include/problems.rs b/rust/src/include/problems.rs index c05409e8..16370251 100644 --- a/rust/src/include/problems.rs +++ b/rust/src/include/problems.rs @@ -18,6 +18,7 @@ use crate::p0069::p0069; use crate::p0076::p0076; use crate::p0077::p0077; use crate::p0087::p0087; +use crate::p0111::p0111; use crate::p0187::p0187; use crate::p0357::p0357; use crate::p0836::p0836; @@ -50,6 +51,7 @@ seq!(N in 0001..=0020 { 76 => Some(( &76, p0076, false)), 77 => Some(( &77, p0077, false)), 87 => Some(( &87, p0087, false)), + 111 => Some((&111, p0111, false)), 187 => Some((&187, p0187, false)), 357 => Some((&357, p0357, true)), 836 => Some((&836, p0836, false)), diff --git a/rust/src/lib.rs b/rust/src/lib.rs index b0406d26..3e15f0ed 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -23,6 +23,7 @@ pub mod p0069; pub mod p0076; pub mod p0077; pub mod p0087; +pub mod p0111; pub mod p0187; pub mod p0357; pub mod p0836; diff --git a/rust/src/main.rs b/rust/src/main.rs index cbdc94a5..675f21e2 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -40,6 +40,7 @@ pub mod p0069; pub mod p0076; pub mod p0077; pub mod p0087; +pub mod p0111; pub mod p0187; pub mod p0357; pub mod p0836; @@ -97,6 +98,7 @@ seq!(N in 01..=20 { #[case::problem_76(76)] #[case::problem_77(77)] #[case::problem_87(87)] +#[case::problem_111(111)] #[case::problem_187(187)] // #[case::problem_357(357)] #[case::problem_836(836)] diff --git a/rust/src/p0111.rs b/rust/src/p0111.rs new file mode 100644 index 00000000..47093045 --- /dev/null +++ b/rust/src/p0111.rs @@ -0,0 +1,22 @@ +/* +Project Euler Problem 111 + +*/ +use std::iter::zip; + +use crate::include::primes::primes_until; +use crate::include::utils::Answer; + +pub fn p0111() -> Answer { + let ten_9: u64 = 10_u64.pow(9); + let ten_10: u64 = 10_u64.pow(10); + let primes_int: Vec = primes_until::(ten_10).filter(|x| *x > ten_9).collect(); + let primes_str: Vec = primes_int.iter().map(|p| (*p).to_string()).collect(); + let mut answer: u64 = 0; + for digit in 0..=9 { + let primes_counts: Vec = primes_str.iter().map(|s| (*s).bytes().filter(|b| *b == digit + b'0').count()).collect(); + let n: usize = primes_counts.iter().map(|x| *x).max().unwrap(); + answer += zip(primes_int.iter(), primes_counts.iter()).filter(|(_i, c)| **c == n).map(|(i, _c)| *i).sum::(); + } + return Answer::Int(answer.into()); +} From d7ddd9f50f89e1c79ac440d5f78cda88bcac97a2 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Thu, 29 Aug 2024 22:31:09 -0500 Subject: [PATCH 2/3] don't collect very large vector --- rust/src/p0111.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/rust/src/p0111.rs b/rust/src/p0111.rs index 47093045..c5b28cc6 100644 --- a/rust/src/p0111.rs +++ b/rust/src/p0111.rs @@ -10,13 +10,20 @@ use crate::include::utils::Answer; pub fn p0111() -> Answer { let ten_9: u64 = 10_u64.pow(9); let ten_10: u64 = 10_u64.pow(10); - let primes_int: Vec = primes_until::(ten_10).filter(|x| *x > ten_9).collect(); - let primes_str: Vec = primes_int.iter().map(|p| (*p).to_string()).collect(); let mut answer: u64 = 0; - for digit in 0..=9 { - let primes_counts: Vec = primes_str.iter().map(|s| (*s).bytes().filter(|b| *b == digit + b'0').count()).collect(); - let n: usize = primes_counts.iter().map(|x| *x).max().unwrap(); - answer += zip(primes_int.iter(), primes_counts.iter()).filter(|(_i, c)| **c == n).map(|(i, _c)| *i).sum::(); + let mut subanswer: Vec = vec![0; 10]; + let mut current: Vec = vec![0; 10]; + for p in primes_until::(ten_10).filter(|x| *x > ten_9) { + let s = p.to_string(); + for digit in 0..=9 { + let count = s.bytes().filter(|b| *b == digit + b'0').count(); + if count > current[digit] { + current[digit] = count; + subanswer[digit] = p; + } else if count == current[digit] { + subanswer[digit] += p; + } + } } - return Answer::Int(answer.into()); + return Answer::Int(subanswer.into_iter().sum::().into()); } From 890285485e6e6af37162e991e1f11130c81b25f4 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Fri, 30 Aug 2024 08:23:43 -0500 Subject: [PATCH 3/3] usize --- rust/src/p0111.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/rust/src/p0111.rs b/rust/src/p0111.rs index c5b28cc6..b418ff49 100644 --- a/rust/src/p0111.rs +++ b/rust/src/p0111.rs @@ -16,12 +16,13 @@ pub fn p0111() -> Answer { for p in primes_until::(ten_10).filter(|x| *x > ten_9) { let s = p.to_string(); for digit in 0..=9 { + let idx = digit as usize; let count = s.bytes().filter(|b| *b == digit + b'0').count(); - if count > current[digit] { - current[digit] = count; - subanswer[digit] = p; - } else if count == current[digit] { - subanswer[digit] += p; + if count > current[idx] { + current[idx] = count; + subanswer[idx] = p; + } else if count == current[idx] { + subanswer[idx] += p; } } }