From 9c64d77532a7fb18a516c61d1cce9e668b76f6bb Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sun, 1 Sep 2024 12:30:47 +0200 Subject: [PATCH] Wrote program for Day 38 --- README.md | 2 +- .../Day-38_Electronics-Shop/day38/Cargo.toml | 6 ++ .../Day-38_Electronics-Shop/day38/src/lib.rs | 44 ++++++++++++ .../Day-38_Electronics-Shop/day38/src/main.rs | 67 +++++++++++++++++++ .../day38/tests/example.rs | 9 +++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 Week-06/Day-38_Electronics-Shop/day38/Cargo.toml create mode 100644 Week-06/Day-38_Electronics-Shop/day38/src/lib.rs create mode 100644 Week-06/Day-38_Electronics-Shop/day38/src/main.rs create mode 100644 Week-06/Day-38_Electronics-Shop/day38/tests/example.rs diff --git a/README.md b/README.md index 3143d1c..da388e8 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #35 | [Dog And Gopher](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-35_Dog-And-Gopher) | :white_check_mark: | | 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_large_square: | +| 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 #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: | diff --git a/Week-06/Day-38_Electronics-Shop/day38/Cargo.toml b/Week-06/Day-38_Electronics-Shop/day38/Cargo.toml new file mode 100644 index 0000000..a1fd99c --- /dev/null +++ b/Week-06/Day-38_Electronics-Shop/day38/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day38" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-06/Day-38_Electronics-Shop/day38/src/lib.rs b/Week-06/Day-38_Electronics-Shop/day38/src/lib.rs new file mode 100644 index 0000000..6ecfaa2 --- /dev/null +++ b/Week-06/Day-38_Electronics-Shop/day38/src/lib.rs @@ -0,0 +1,44 @@ +pub fn get_money_spent( + keyboards: &[usize], + mut drives: Vec, + budget: usize, +) -> Option { + let mut result = None; + drives.sort(); + + for k in keyboards { + match floor(&drives, budget - k) { + Some(d) => { + if result.is_none() || Some(k + d) > result { + result = Some(k + d) + } + } + None => continue, + } + } + + result +} + +fn floor(array: &[usize], element: usize) -> Option { + let mut start = 0; + let mut end = array.len() - 1; + + while start <= end { + println!("start: {start}, end: {end}"); + let mid = (start + end) / 2; + match array[mid].cmp(&element) { + std::cmp::Ordering::Equal => return Some(element), + std::cmp::Ordering::Less => start = mid + 1, + std::cmp::Ordering::Greater => { + if mid > 0 { + end = mid - 1 + } else { + return None; + } + } + } + } + + Some(array[start - 1]) +} diff --git a/Week-06/Day-38_Electronics-Shop/day38/src/main.rs b/Week-06/Day-38_Electronics-Shop/day38/src/main.rs new file mode 100644 index 0000000..24d9ecb --- /dev/null +++ b/Week-06/Day-38_Electronics-Shop/day38/src/main.rs @@ -0,0 +1,67 @@ +use std::{ + io::{self, Write}, + num::ParseIntError, + process::exit, +}; + +use day38::get_money_spent; + +fn get_string(request: &str) -> String { + print!("{request}"); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut buffer = String::new(); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + buffer +} + +fn get_usize_vec(request: &str) -> Result, String> { + let buffer = get_string(request); + Ok(buffer + .split_whitespace() + .try_fold(Vec::new(), |mut acc, x| match x.parse::() { + Ok(n) => { + acc.push(n); + Ok(acc) + } + Err(_) => Err("Prices must be valid integers".to_string()), + }) + .unwrap_or(Vec::new())) +} + +fn get_usize(request: &str) -> Result { + let buffer = get_string(request); + buffer.parse() +} + +fn main() { + let keyboards = match get_usize_vec("Insert the price of the keyboards space separated: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + let drives = match get_usize_vec("Insert the price of the drivers space separated: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; + let budget = match get_usize("Insert the budget: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{}", e); + exit(1); + } + }; + let result = get_money_spent(&keyboards, drives, budget); + match result { + Some(x) => println!("The maximum that can be spent is {x}."), + None => println!("It is not possible to buy both items."), + } +} diff --git a/Week-06/Day-38_Electronics-Shop/day38/tests/example.rs b/Week-06/Day-38_Electronics-Shop/day38/tests/example.rs new file mode 100644 index 0000000..17bfcdd --- /dev/null +++ b/Week-06/Day-38_Electronics-Shop/day38/tests/example.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod example { + use day38::get_money_spent; + + #[test] + fn test() { + assert_eq!(get_money_spent(&[40, 50, 60], vec![5, 8, 12], 60), Some(58)); + } +}