diff --git a/README.md b/README.md index df8cd24..1f56a9e 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #14 | [The Karacas Encryption Algorithm](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-14_Karacas-Encryption-Algorithm) | :white_check_mark: | | Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_check_mark: | | Day #16 | [Nim Game](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-16_Nim-Game) | :white_check_mark: | -| Day #17 | [Prison Break](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-17_Prison-Break) | :white_large_square: | +| Day #17 | [Prison Break](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-17_Prison-Break) | :white_check_mark: | | Day #18 | [Unique Paths](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-18_Unique-Paths) | :white_large_square: | | Day #19 | [URL Shortener](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-19_URL-Shortener) | :white_large_square: | | Day #20 | [API Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-20_API-Challenge) | :white_large_square: | diff --git a/Week-03/Day-17_Prison-Break/day17/Cargo.toml b/Week-03/Day-17_Prison-Break/day17/Cargo.toml new file mode 100644 index 0000000..7d3f4fa --- /dev/null +++ b/Week-03/Day-17_Prison-Break/day17/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day17" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-03/Day-17_Prison-Break/day17/src/lib.rs b/Week-03/Day-17_Prison-Break/day17/src/lib.rs new file mode 100644 index 0000000..40f5d3a --- /dev/null +++ b/Week-03/Day-17_Prison-Break/day17/src/lib.rs @@ -0,0 +1,17 @@ +pub fn freed_prisoners(cells: &[bool]) -> usize { + if !cells[0] { + return 0; + } + + let mut count = 0; + let mut required = true; + + for c in cells { + if c == &required { + count += 1; + required = !required; + } + } + + count +} diff --git a/Week-03/Day-17_Prison-Break/day17/src/main.rs b/Week-03/Day-17_Prison-Break/day17/src/main.rs new file mode 100644 index 0000000..4bf5254 --- /dev/null +++ b/Week-03/Day-17_Prison-Break/day17/src/main.rs @@ -0,0 +1,27 @@ +use day17::freed_prisoners; +use std::io::{self, Write}; + +fn main() { + let mut buffer = String::new(); + + print!("Enter the cells (0s and 1s space separated): "); + io::stdout().flush().expect("Failed to flush stdout"); + + std::io::stdin() + .read_line(&mut buffer) + .expect("Failed to read from stdin"); + + let cells: Vec = buffer + .split_whitespace() + .map( + |x| match x.parse::().expect("Only numbers are expected") { + 0 => false, + 1 => true, + _ => panic!("Only 0s and 1s are allowed"), + }, + ) + .collect(); + let result = freed_prisoners(&cells); + + println!("You can free {} prisoners", result); +} diff --git a/Week-03/Day-17_Prison-Break/day17/tests/examples.rs b/Week-03/Day-17_Prison-Break/day17/tests/examples.rs new file mode 100644 index 0000000..93cc371 --- /dev/null +++ b/Week-03/Day-17_Prison-Break/day17/tests/examples.rs @@ -0,0 +1,24 @@ +use day17::freed_prisoners; + +#[test] +fn example1() { + assert_eq!( + freed_prisoners(&[true, true, false, false, false, true, false]), + 4 + ); +} + +#[test] +fn example2() { + assert_eq!(freed_prisoners(&[true, true, true]), 1); +} + +#[test] +fn example3() { + assert_eq!(freed_prisoners(&[false, false, false]), 0); +} + +#[test] +fn example4() { + assert_eq!(freed_prisoners(&[false, true, true, true]), 0); +}