diff --git a/README.md b/README.md index 5d0fa0b..cb647b3 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_check_mark: | | Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_check_mark: | | Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: | -| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_large_square: | +| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_check_mark: | | Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_large_square: | | Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: | | Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_large_square: | diff --git a/Week-04/Day-25_Coin-Trouble/day25/Cargo.toml b/Week-04/Day-25_Coin-Trouble/day25/Cargo.toml new file mode 100644 index 0000000..e5f4cc3 --- /dev/null +++ b/Week-04/Day-25_Coin-Trouble/day25/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day25" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-04/Day-25_Coin-Trouble/day25/src/lib.rs b/Week-04/Day-25_Coin-Trouble/day25/src/lib.rs new file mode 100644 index 0000000..14c290c --- /dev/null +++ b/Week-04/Day-25_Coin-Trouble/day25/src/lib.rs @@ -0,0 +1,35 @@ +pub fn coins_div(coins: &[usize]) -> bool { + let sum: usize = coins.iter().copied().sum(); + let each = sum / 3; + + if sum % 3 != 0 { + false + } else { + try_coins_div(coins, 0, each) + } +} + +fn try_coins_div(coins: &[usize], current_coins: usize, should_have: usize) -> bool { + if coins.is_empty() { + return current_coins == 0; + } + + for (i, c) in coins.iter().enumerate() { + let coins_without_current = [&coins[..i], &coins[i + 1..]].concat(); + if current_coins + c <= should_have + && try_coins_div( + &coins_without_current, + if current_coins + c == should_have { + 0 + } else { + current_coins + c + }, + should_have, + ) + { + return true; + } + } + + false +} diff --git a/Week-04/Day-25_Coin-Trouble/day25/src/main.rs b/Week-04/Day-25_Coin-Trouble/day25/src/main.rs new file mode 100644 index 0000000..bc21b20 --- /dev/null +++ b/Week-04/Day-25_Coin-Trouble/day25/src/main.rs @@ -0,0 +1,25 @@ +use std::io::{self, Write}; + +use day25::coins_div; + +fn main() { + let mut buffer = String::new(); + + print!("Insert the value of the coins space separated: "); + io::stdout().flush().expect("Failed to flush stdout"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let coins: Vec = buffer + .split_whitespace() + .map(|x| x.parse().expect("Input must be made of numbers")) + .collect(); + + if coins_div(&coins) { + println!("The coins can be divided equally") + } else { + println!("The coins can't be divided equally"); + } +} diff --git a/Week-04/Day-25_Coin-Trouble/day25/tests/examples.rs b/Week-04/Day-25_Coin-Trouble/day25/tests/examples.rs new file mode 100644 index 0000000..7e8530e --- /dev/null +++ b/Week-04/Day-25_Coin-Trouble/day25/tests/examples.rs @@ -0,0 +1,18 @@ +#[cfg(test)] +mod examples { + use day25::coins_div; + + #[test] + fn example1() { + assert!(coins_div(&[1, 2, 3, 2, 2, 2, 3])); + } + + #[test] + fn example2() { + assert!(!coins_div(&[5, 3, 10, 1, 2])); + } + #[test] + fn example3() { + assert!(coins_div(&[2, 4, 3, 2, 4, 9, 7, 8, 6, 9])); + } +}