Wrote program for Day 39

This commit is contained in:
Mariano Riefolo 2024-09-02 08:39:20 +02:00
parent 9c64d77532
commit 311260e8a1
5 changed files with 86 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@ -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<usize, ParseIntError> {
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!");
}

View File

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