diff --git a/README.md b/README.md index 5e68afb..709ff4c 100644 --- a/README.md +++ b/README.md @@ -109,7 +109,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_check_mark: | | Day #61 | [Write A Web Crawler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-61_Write-A-Web-Crawler) | :white_check_mark: | | Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_check_mark: | -| Day #63 | [The Rabbit Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-63_The-Rabbit-Problem) | :white_large_square: | +| Day #63 | [The Rabbit Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-63_The-Rabbit-Problem) | :white_check_mark: | | Day #64 | [First Recurring Character](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-64_First-Recurring-Character) | :white_large_square: | | Day #65 | [ISBN Validator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-65_ISBN-Validator) | :white_large_square: | | Day #66 | [ISBN Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-66_ISBN-Generator) | :white_large_square: | diff --git a/Week-09/Day-63_The-Rabbit-Problem/day63/Cargo.toml b/Week-09/Day-63_The-Rabbit-Problem/day63/Cargo.toml new file mode 100644 index 0000000..bc903cd --- /dev/null +++ b/Week-09/Day-63_The-Rabbit-Problem/day63/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day63" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-09/Day-63_The-Rabbit-Problem/day63/src/lib.rs b/Week-09/Day-63_The-Rabbit-Problem/day63/src/lib.rs new file mode 100644 index 0000000..6caae21 --- /dev/null +++ b/Week-09/Day-63_The-Rabbit-Problem/day63/src/lib.rs @@ -0,0 +1,47 @@ +pub fn rabbit_problem(male: usize, female: usize, needed: usize) -> Result<(usize, usize), String> { + if female == 0 { + return Err("The number of initial female rabbits must be positive".to_owned()); + } + let mut females = [0usize; 97]; + let mut males = [0usize; 97]; + let mut count = male + female; + let mut months = 1; + let mut deaths = 0; + females[3] = female; + males[3] = male; + while count < needed { + let mut males_newborns = 0; + let mut females_newborns = 0; + let mut current_month_deaths = 0; + + for i in (0..97).rev() { + if i >= 4 { + males_newborns += 5 * females[i]; + females_newborns += 9 * females[i]; + } + if i == 96 { + current_month_deaths = males[i] + females[i]; + males[i] = 0; + females[i] = 0; + continue; + } + males[i + 1] = males[i]; + females[i + 1] = females[i]; + } + + count = match count.checked_add( + match males_newborns.checked_add(females_newborns - current_month_deaths) { + Some(x) => x, + None => needed, + }, + ) { + Some(x) => x, + None => needed, + }; + deaths += current_month_deaths; + females[0] = females_newborns; + males[0] = males_newborns; + months += 1; + } + Ok((months, deaths)) +} diff --git a/Week-09/Day-63_The-Rabbit-Problem/day63/src/main.rs b/Week-09/Day-63_The-Rabbit-Problem/day63/src/main.rs new file mode 100644 index 0000000..ba44355 --- /dev/null +++ b/Week-09/Day-63_The-Rabbit-Problem/day63/src/main.rs @@ -0,0 +1,56 @@ +use std::{ + io::{self, Write}, + num::ParseIntError, + process::exit, +}; + +use day63::rabbit_problem; + +fn read_usize() -> Result { + let mut buffer = String::new(); + io::stdin().read_line(&mut buffer).unwrap(); + buffer.trim().parse() +} + +fn main() { + print!("Enter the number of male rabbits: "); + io::stdout().flush().unwrap(); + let male = match read_usize() { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + + print!("Enter the number of female rabbits: "); + io::stdout().flush().unwrap(); + let female = match read_usize() { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + + print!("Enter the needed number of rabbits: "); + io::stdout().flush().unwrap(); + let needed = match read_usize() { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + + let (months, dead) = match rabbit_problem(male, female, needed) { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + println!( + "After {months} months the rabbits dominated the world, during which {dead} rabbits died" + ); +} diff --git a/Week-09/Day-63_The-Rabbit-Problem/day63/tests/examples.rs b/Week-09/Day-63_The-Rabbit-Problem/day63/tests/examples.rs new file mode 100644 index 0000000..4f9aebc --- /dev/null +++ b/Week-09/Day-63_The-Rabbit-Problem/day63/tests/examples.rs @@ -0,0 +1,14 @@ +#[cfg(test)] +mod examples { + use day63::rabbit_problem; + + #[test] + fn example1() { + assert_eq!(rabbit_problem(2, 4, 1000000000).unwrap().0, 32) + } + + #[test] + fn example2() { + assert_eq!(rabbit_problem(2, 4, 15000000000).unwrap().0, 36) + } +}