feat: 19 - counting sundays
This commit is contained in:
parent
7945bdd0ce
commit
adaa608e31
4 changed files with 53 additions and 10 deletions
|
@ -16,7 +16,7 @@ All of the solutions here are written in rust. [main.rs](src/main.rs) is the hom
|
||||||
|
|
||||||
## Challenge Completion
|
## 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] 1 - [Multiples of 3 or 5](src/bin/1.rs)
|
||||||
- [x] 2 - [Even Fibonacci numbers](src/bin/2.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] 16 - [Power digit sum](src/bin/16.rs)
|
||||||
- [x] 17 - [Number letter counts](src/bin/17.rs)
|
- [x] 17 - [Number letter counts](src/bin/17.rs)
|
||||||
- [x] 18 - [Maximum path sum I](src/bin/18.rs)
|
- [x] 18 - [Maximum path sum I](src/bin/18.rs)
|
||||||
- [ ] 19 - Counting Sundays
|
- [x] 19 - Counting Sundays
|
||||||
- [ ] 20 - Factorial digit sum
|
- [ ] 20 - Factorial digit sum
|
||||||
- [ ] 21 - Amicable numbers
|
- [ ] 21 - Amicable numbers
|
||||||
- [x] 22 - [Names scores](src/bin/22.rs)
|
- [x] 22 - [Names scores](src/bin/22.rs)
|
||||||
|
|
40
src/bin/19.rs
Normal file
40
src/bin/19.rs
Normal file
|
@ -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);
|
||||||
|
}
|
|
@ -20,6 +20,8 @@ mod four;
|
||||||
mod fourteen;
|
mod fourteen;
|
||||||
#[path = "../bin/9.rs"]
|
#[path = "../bin/9.rs"]
|
||||||
mod nine;
|
mod nine;
|
||||||
|
#[path = "../bin/19.rs"]
|
||||||
|
mod nineteen;
|
||||||
#[path = "../bin/1.rs"]
|
#[path = "../bin/1.rs"]
|
||||||
mod one;
|
mod one;
|
||||||
#[path = "../bin/7.rs"]
|
#[path = "../bin/7.rs"]
|
||||||
|
@ -84,6 +86,7 @@ pub async fn execute(
|
||||||
22 => twenty_two::main(),
|
22 => twenty_two::main(),
|
||||||
27 => twenty_seven::main(),
|
27 => twenty_seven::main(),
|
||||||
67 => sixty_seven::main(),
|
67 => sixty_seven::main(),
|
||||||
|
19 => nineteen::main(),
|
||||||
_ => {
|
_ => {
|
||||||
exists = false;
|
exists = false;
|
||||||
println!(
|
println!(
|
||||||
|
|
|
@ -64,7 +64,7 @@ impl Problem {
|
||||||
let number = number
|
let number = number
|
||||||
.unwrap_or_else(|| Self::prompt_number("Please select a problem:", false).unwrap());
|
.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?
|
.await?
|
||||||
.text()
|
.text()
|
||||||
.await?;
|
.await?;
|
||||||
|
@ -105,14 +105,14 @@ impl Problem {
|
||||||
pub fn file_body(&self) -> String {
|
pub fn file_body(&self) -> String {
|
||||||
format!(
|
format!(
|
||||||
"/*
|
"/*
|
||||||
Problem {} - {}
|
Problem {} - {}
|
||||||
|
|
||||||
{}
|
{}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
pub fn main() {{
|
pub fn main() {{
|
||||||
println!(\"Hello World!\");
|
println!(\"Hello World!\");
|
||||||
}}",
|
}}",
|
||||||
self.number,
|
self.number,
|
||||||
html_escape::decode_html_entities(&self.title).to_string(),
|
html_escape::decode_html_entities(&self.title).to_string(),
|
||||||
html_escape::decode_html_entities(&self.content)
|
html_escape::decode_html_entities(&self.content)
|
||||||
|
|
Loading…
Reference in a new issue