From dfc105e2c8aa8fb17bd68f67390e3621f9b8b503 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Thu, 12 Sep 2024 11:51:31 +0200 Subject: [PATCH] Wrote program for Day 49 --- README.md | 2 +- Week-07/Day-49_Swimming-Pool/day49/Cargo.toml | 6 ++ Week-07/Day-49_Swimming-Pool/day49/src/lib.rs | 19 ++++++ .../Day-49_Swimming-Pool/day49/src/main.rs | 60 +++++++++++++++++++ .../day49/tests/examples.rs | 43 +++++++++++++ 5 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 Week-07/Day-49_Swimming-Pool/day49/Cargo.toml create mode 100644 Week-07/Day-49_Swimming-Pool/day49/src/lib.rs create mode 100644 Week-07/Day-49_Swimming-Pool/day49/src/main.rs create mode 100644 Week-07/Day-49_Swimming-Pool/day49/tests/examples.rs diff --git a/README.md b/README.md index 637288b..5a0adbb 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #46 | [Hot Pics Of Danny Devito](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-46_Hot-Pics-Of-Danny-Devito) | :white_check_mark: | | Day #47 | [Zip It](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-47_Zip-It) | :white_check_mark: | | Day #48 | [Christmas Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-48_Christmas-Tree) | :white_check_mark: | -| Day #49 | [Swimming Pool](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-49_Swimming-Pool) | :white_large_square: | +| Day #49 | [Swimming Pool](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-49_Swimming-Pool) | :white_check_mark: | | Day #50 | [Tic Tac Toe](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-50_Tic-Tac-Toe) | :white_large_square: | | Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_large_square: | | Day #52 | [Switch On The Gravity](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-52_Switch-On-The-Gravity) | :white_large_square: | diff --git a/Week-07/Day-49_Swimming-Pool/day49/Cargo.toml b/Week-07/Day-49_Swimming-Pool/day49/Cargo.toml new file mode 100644 index 0000000..f110c02 --- /dev/null +++ b/Week-07/Day-49_Swimming-Pool/day49/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day49" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-07/Day-49_Swimming-Pool/day49/src/lib.rs b/Week-07/Day-49_Swimming-Pool/day49/src/lib.rs new file mode 100644 index 0000000..73cf0b4 --- /dev/null +++ b/Week-07/Day-49_Swimming-Pool/day49/src/lib.rs @@ -0,0 +1,19 @@ +pub fn is_legitimate(pool: &[Vec]) -> Option { + for row in pool { + if *row.first()? == 1 || *row.last()? == 1 { + return Some(false); + } + } + for first_row in pool.first()? { + if *first_row == 1 { + return Some(false); + } + } + for last_row in pool.last()? { + if *last_row == 1 { + return Some(false); + } + } + + Some(true) +} diff --git a/Week-07/Day-49_Swimming-Pool/day49/src/main.rs b/Week-07/Day-49_Swimming-Pool/day49/src/main.rs new file mode 100644 index 0000000..731140b --- /dev/null +++ b/Week-07/Day-49_Swimming-Pool/day49/src/main.rs @@ -0,0 +1,60 @@ +use std::io; +use std::process::exit; + +use day49::is_legitimate; + +fn read_row() -> Result, Box> { + let mut buffer = String::new(); + io::stdin().read_line(&mut buffer)?; + + let row: Vec = buffer + .split_whitespace() + .map(|x: &str| match x.parse::() { + Ok(x) => match x { + 0 | 1 => Ok(x), + _ => Err("Only 0 and 1 are accepted".to_string()), + }, + Err(_) => Err("Only numbers are accepted".to_string()), + }) + .collect::, String>>()?; + + if row.is_empty() { + Err("Empty row")? + } + + Ok(row) +} + +fn read_pool() -> Result>, Box> { + let mut pool = vec![read_row()?]; + while let Ok(row) = read_row() { + if row.len() != pool.first().unwrap().len() { + Err("Invalid row length")? + } + pool.push(row); + } + Ok(pool) +} + +fn main() { + println!("Insert the pool (space-separated 0s and 1s, one row per line)"); + println!("End input with an empty line"); + + let pool = match read_pool() { + Ok(x) => x, + Err(e) => { + eprintln!("Error: {e}"); + exit(1); + } + }; + + if let Some(legit) = is_legitimate(&pool) { + if legit { + println!("The pool is legitimate"); + } else { + println!("The pool is not legitimate"); + } + } else { + println!("Invalid pool"); + } +} diff --git a/Week-07/Day-49_Swimming-Pool/day49/tests/examples.rs b/Week-07/Day-49_Swimming-Pool/day49/tests/examples.rs new file mode 100644 index 0000000..4db1cc3 --- /dev/null +++ b/Week-07/Day-49_Swimming-Pool/day49/tests/examples.rs @@ -0,0 +1,43 @@ +#[cfg(test)] +mod examples { + use day49::is_legitimate; + + #[test] + fn example1() { + assert_eq!( + is_legitimate(&[ + vec![0, 0, 0, 0, 0, 0, 0, 0], + vec![0, 0, 1, 1, 1, 0, 0, 0], + vec![0, 1, 1, 1, 1, 1, 0, 0], + vec![0, 0, 0, 0, 0, 0, 0, 0] + ]), + Some(true) + ); + } + + #[test] + fn example2() { + assert_eq!( + is_legitimate(&[ + vec![0, 0, 0, 0, 0, 0, 0, 0], + vec![0, 0, 1, 1, 1, 0, 0, 0], + vec![0, 1, 1, 1, 1, 1, 0, 0], + vec![0, 0, 1, 1, 1, 0, 0, 0] + ]), + Some(false) + ); + } + + #[test] + fn example3() { + assert_eq!( + is_legitimate(&[ + vec![0, 0, 0, 0, 0], + vec![0, 1, 1, 1, 0], + vec![0, 1, 1, 1, 0], + vec![0, 0, 0, 0, 0] + ]), + Some(true) + ); + } +}