diff --git a/README.md b/README.md index cb647b3..3981ebf 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | 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_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 #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_check_mark: | | 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: | | Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_large_square: | diff --git a/Week-04/Day-26_Briefcase-Lock/day26/Cargo.toml b/Week-04/Day-26_Briefcase-Lock/day26/Cargo.toml new file mode 100644 index 0000000..95c7115 --- /dev/null +++ b/Week-04/Day-26_Briefcase-Lock/day26/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day26" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-04/Day-26_Briefcase-Lock/day26/src/lib.rs b/Week-04/Day-26_Briefcase-Lock/day26/src/lib.rs new file mode 100644 index 0000000..5632f4a --- /dev/null +++ b/Week-04/Day-26_Briefcase-Lock/day26/src/lib.rs @@ -0,0 +1,13 @@ +pub fn min_turns(current_lock: &str, target_lock: &str) -> u8 { + let mut result = 0; + + for (i, c) in current_lock.bytes().enumerate() { + let tl = target_lock.as_bytes()[i]; + result += std::cmp::min( + (10 + (c - b'0') - (tl - b'0')) % 10, + (10 + (tl - b'0') - (c - b'0')) % 10, + ); + } + + result +} diff --git a/Week-04/Day-26_Briefcase-Lock/day26/src/main.rs b/Week-04/Day-26_Briefcase-Lock/day26/src/main.rs new file mode 100644 index 0000000..1c12348 --- /dev/null +++ b/Week-04/Day-26_Briefcase-Lock/day26/src/main.rs @@ -0,0 +1,24 @@ +use std::io::{self, Write}; + +use day26::min_turns; + +fn read_lock(request: &str) -> String { + let mut buffer = String::new(); + + print!("{}", request); + io::stdout().flush().expect("Failed to flush"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + buffer.trim().to_owned() +} + +fn main() { + let current_lock = read_lock("Insert the current lock: "); + let target_lock = read_lock("Insert the target lock: "); + + let result = min_turns(¤t_lock, &target_lock); + println!("The minimum number of turns required is {}", result); +} diff --git a/Week-04/Day-26_Briefcase-Lock/day26/tests/examples.rs b/Week-04/Day-26_Briefcase-Lock/day26/tests/examples.rs new file mode 100644 index 0000000..08417af --- /dev/null +++ b/Week-04/Day-26_Briefcase-Lock/day26/tests/examples.rs @@ -0,0 +1,19 @@ +#[cfg(test)] +mod examples { + use day26::min_turns; + + #[test] + fn example1() { + assert_eq!(min_turns("4089", "5672"), 9); + } + + #[test] + fn example2() { + assert_eq!(min_turns("1111", "1100"), 2); + } + + #[test] + fn example3() { + assert_eq!(min_turns("2391", "4984"), 10); + } +}