From adaa608e31f0a5b75055fc9fb27081d6c88ba055 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:10:12 +0100 Subject: [PATCH] feat: 19 - counting sundays --- readme.md | 4 ++-- src/bin/19.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/commands/run.rs | 3 +++ src/problem.rs | 16 ++++++++-------- 4 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 src/bin/19.rs diff --git a/readme.md b/readme.md index 2c82865..8874073 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,7 @@ All of the solutions here are written in rust. [main.rs](src/main.rs) is the hom ## Challenge Completion -### 22 out of 100 public challenges completed. +### 23 out of 100 public challenges completed. - [x] 1 - [Multiples of 3 or 5](src/bin/1.rs) - [x] 2 - [Even Fibonacci numbers](src/bin/2.rs) @@ -36,7 +36,7 @@ All of the solutions here are written in rust. [main.rs](src/main.rs) is the hom - [x] 16 - [Power digit sum](src/bin/16.rs) - [x] 17 - [Number letter counts](src/bin/17.rs) - [x] 18 - [Maximum path sum I](src/bin/18.rs) -- [ ] 19 - Counting Sundays +- [x] 19 - Counting Sundays - [ ] 20 - Factorial digit sum - [ ] 21 - Amicable numbers - [x] 22 - [Names scores](src/bin/22.rs) diff --git a/src/bin/19.rs b/src/bin/19.rs new file mode 100644 index 0000000..099f681 --- /dev/null +++ b/src/bin/19.rs @@ -0,0 +1,40 @@ +/* +Problem 19 - Counting Sundays + +You are given the following information, but you may prefer to do some research for yourself. +1 Jan 1900 was a Monday. +Thirty days has September, +April, June and November. +All the rest have thirty-one, +Saving February alone, +Which has twenty-eight, rain or shine. +And on leap years, twenty-nine. +A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. +How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? +*/ +fn is_leap_year(year: u16) -> bool { + (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 +} + +pub fn main() { + let mut sundays_on_first = 0; + let days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; + let mut current_day = 1; // January 1, 1901 was a Tuesday, so starting with 1 (Tuesday) + + for year in 1901..=2000 { + for (month, &days) in days_in_month.iter().enumerate() { + if month == 1 && is_leap_year(year) { + current_day = (current_day + 29) % 7; // Leap year + } else { + current_day = (current_day + days) % 7; // Regular year + } + + if current_day == 0 { + // Checking if the first day of the month is a Sunday (6 represents Sunday) + sundays_on_first += 1; + } + } + } + + println!("The amount of Sundays that fell on the first of the month during the twentieth century is {}.", sundays_on_first); +} diff --git a/src/commands/run.rs b/src/commands/run.rs index ef896ae..faf8dc9 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -20,6 +20,8 @@ mod four; mod fourteen; #[path = "../bin/9.rs"] mod nine; +#[path = "../bin/19.rs"] +mod nineteen; #[path = "../bin/1.rs"] mod one; #[path = "../bin/7.rs"] @@ -84,6 +86,7 @@ pub async fn execute( 22 => twenty_two::main(), 27 => twenty_seven::main(), 67 => sixty_seven::main(), + 19 => nineteen::main(), _ => { exists = false; println!( diff --git a/src/problem.rs b/src/problem.rs index 0d8dfc3..af10ee0 100644 --- a/src/problem.rs +++ b/src/problem.rs @@ -64,7 +64,7 @@ impl Problem { let number = number .unwrap_or_else(|| Self::prompt_number("Please select a problem:", false).unwrap()); - let body = reqwest::get(format!("https://projecteuler.net/problem={number}")) + let body = reqwest::get(format!("https://projecteuler.net/minimal={number}")) .await? .text() .await?; @@ -105,14 +105,14 @@ impl Problem { pub fn file_body(&self) -> String { format!( "/* - Problem {} - {} +Problem {} - {} + +{} +*/ - {} - */ - - pub fn main() {{ - println!(\"Hello World!\"); - }}", +pub fn main() {{ + println!(\"Hello World!\"); +}}", self.number, html_escape::decode_html_entities(&self.title).to_string(), html_escape::decode_html_entities(&self.content)