Wrote program for Day 49
This commit is contained in:
parent
a4927ec7a7
commit
dfc105e2c8
@ -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: |
|
||||
|
6
Week-07/Day-49_Swimming-Pool/day49/Cargo.toml
Normal file
6
Week-07/Day-49_Swimming-Pool/day49/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day49"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
19
Week-07/Day-49_Swimming-Pool/day49/src/lib.rs
Normal file
19
Week-07/Day-49_Swimming-Pool/day49/src/lib.rs
Normal file
@ -0,0 +1,19 @@
|
||||
pub fn is_legitimate(pool: &[Vec<u8>]) -> Option<bool> {
|
||||
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)
|
||||
}
|
60
Week-07/Day-49_Swimming-Pool/day49/src/main.rs
Normal file
60
Week-07/Day-49_Swimming-Pool/day49/src/main.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use std::io;
|
||||
use std::process::exit;
|
||||
|
||||
use day49::is_legitimate;
|
||||
|
||||
fn read_row() -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
||||
let mut buffer = String::new();
|
||||
io::stdin().read_line(&mut buffer)?;
|
||||
|
||||
let row: Vec<u8> = buffer
|
||||
.split_whitespace()
|
||||
.map(|x: &str| match x.parse::<u8>() {
|
||||
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::<Result<Vec<u8>, String>>()?;
|
||||
|
||||
if row.is_empty() {
|
||||
Err("Empty row")?
|
||||
}
|
||||
|
||||
Ok(row)
|
||||
}
|
||||
|
||||
fn read_pool() -> Result<Vec<Vec<u8>>, Box<dyn std::error::Error>> {
|
||||
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");
|
||||
}
|
||||
}
|
43
Week-07/Day-49_Swimming-Pool/day49/tests/examples.rs
Normal file
43
Week-07/Day-49_Swimming-Pool/day49/tests/examples.rs
Normal file
@ -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)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user