Wrote program for Day 63

This commit is contained in:
Mariano Riefolo 2024-09-26 10:52:38 +02:00
parent 09dccbbc8a
commit 72ad5ee38a
5 changed files with 124 additions and 1 deletions

View File

@ -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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day63"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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))
}

View File

@ -0,0 +1,56 @@
use std::{
io::{self, Write},
num::ParseIntError,
process::exit,
};
use day63::rabbit_problem;
fn read_usize() -> Result<usize, ParseIntError> {
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"
);
}

View File

@ -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)
}
}