diff --git a/README.md b/README.md index da388e8..4687673 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #36 | [LCD Display](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-36_LCD-Display) | :white_check_mark: | | Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_check_mark: | | Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_check_mark: | -| Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_large_square: | +| Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_check_mark: | | Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_large_square: | | Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_large_square: | | Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_large_square: | diff --git a/Week-06/Day-39_Halloween-Sale/day39/Cargo.toml b/Week-06/Day-39_Halloween-Sale/day39/Cargo.toml new file mode 100644 index 0000000..cca1f91 --- /dev/null +++ b/Week-06/Day-39_Halloween-Sale/day39/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day39" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-06/Day-39_Halloween-Sale/day39/src/lib.rs b/Week-06/Day-39_Halloween-Sale/day39/src/lib.rs new file mode 100644 index 0000000..2bd2f8c --- /dev/null +++ b/Week-06/Day-39_Halloween-Sale/day39/src/lib.rs @@ -0,0 +1,15 @@ +pub fn how_many_games(price: usize, discount: usize, min_price: usize, budget: usize) -> usize { + let mut games = 0; + let mut spent = 0; + + while min_price < price - std::cmp::min(price, discount * games) { + spent += price - std::cmp::min(price, discount * games); + games += 1; + } + + if min_price >= price - std::cmp::min(price, discount * games) { + games += (budget - spent) / min_price; + } + + games +} diff --git a/Week-06/Day-39_Halloween-Sale/day39/src/main.rs b/Week-06/Day-39_Halloween-Sale/day39/src/main.rs new file mode 100644 index 0000000..8644cdb --- /dev/null +++ b/Week-06/Day-39_Halloween-Sale/day39/src/main.rs @@ -0,0 +1,55 @@ +use std::process::exit; + +use std::{ + io::{self, Write}, + num::ParseIntError, +}; + +use day39::how_many_games; + +fn read_usize(request: &str) -> Result { + let mut buffer = String::new(); + + print!("{request}"); + io::stdout().flush().expect("Failed to flush stdout"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + buffer.trim().parse() +} + +fn main() { + let price = match read_usize("Insert the normal price: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + let discount = match read_usize("Insert the discount: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + let min_price = match read_usize("Insert the minimum price: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + let budget = match read_usize("Insert the budget: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + + let result = how_many_games(price, discount, min_price, budget); + println!("You can buy {result} games!"); +} diff --git a/Week-06/Day-39_Halloween-Sale/day39/tests/example.rs b/Week-06/Day-39_Halloween-Sale/day39/tests/example.rs new file mode 100644 index 0000000..0dd9967 --- /dev/null +++ b/Week-06/Day-39_Halloween-Sale/day39/tests/example.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod example { + use day39::how_many_games; + + #[test] + fn example() { + assert_eq!(how_many_games(20, 3, 6, 80), 6); + } +}