From c967ea8a12e78f3b1825e30be5e74e3ca83d26e7 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sun, 15 Sep 2024 12:32:02 +0200 Subject: [PATCH] Wrote program for Day 52 --- README.md | 2 +- .../day52/Cargo.toml | 6 +++ .../day52/src/lib.rs | 22 ++++++++ .../day52/src/main.rs | 45 ++++++++++++++++ .../day52/tests/examples.rs | 53 +++++++++++++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 Week-08/Day-52_Switch-On-The-Gravity/day52/Cargo.toml create mode 100644 Week-08/Day-52_Switch-On-The-Gravity/day52/src/lib.rs create mode 100644 Week-08/Day-52_Switch-On-The-Gravity/day52/src/main.rs create mode 100644 Week-08/Day-52_Switch-On-The-Gravity/day52/tests/examples.rs diff --git a/README.md b/README.md index d55dbe0..c384cbd 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | 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_check_mark: | | Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_check_mark: | -| 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: | +| 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_check_mark: | | Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_large_square: | | Day #54 | [RGB To Hex Color Convertor](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-54_RGB-To-Hex-Color-Converter) | :white_large_square: | | Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_large_square: | diff --git a/Week-08/Day-52_Switch-On-The-Gravity/day52/Cargo.toml b/Week-08/Day-52_Switch-On-The-Gravity/day52/Cargo.toml new file mode 100644 index 0000000..3312a0d --- /dev/null +++ b/Week-08/Day-52_Switch-On-The-Gravity/day52/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day52" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-08/Day-52_Switch-On-The-Gravity/day52/src/lib.rs b/Week-08/Day-52_Switch-On-The-Gravity/day52/src/lib.rs new file mode 100644 index 0000000..d54c122 --- /dev/null +++ b/Week-08/Day-52_Switch-On-The-Gravity/day52/src/lib.rs @@ -0,0 +1,22 @@ +pub fn switch_gravity_on(matrix: &Vec>) -> Option>> { + let mut column = vec![0usize; matrix.first()?.len()]; + for row in matrix { + for (i, cell) in row.iter().enumerate() { + if *cell == '#' { + *column.get_mut(i)? += 1; + } + } + } + + let mut final_matrix = matrix.clone(); + for (i, row) in final_matrix.iter_mut().enumerate() { + for (j, cell) in row.iter_mut().enumerate() { + *cell = if i >= matrix.len() - column.get(j)? { + '#' + } else { + '-' + }; + } + } + Some(final_matrix) +} diff --git a/Week-08/Day-52_Switch-On-The-Gravity/day52/src/main.rs b/Week-08/Day-52_Switch-On-The-Gravity/day52/src/main.rs new file mode 100644 index 0000000..5b2a35d --- /dev/null +++ b/Week-08/Day-52_Switch-On-The-Gravity/day52/src/main.rs @@ -0,0 +1,45 @@ +use std::process::exit; + +use day52::switch_gravity_on; + +fn read_matrix() -> Result>, String> { + let mut matrix: Vec> = Vec::new(); + loop { + let mut row = String::new(); + std::io::stdin().read_line(&mut row).unwrap(); + if row.trim().chars().any(|c| c != '#' && c != '-') { + return Err("Invalid character in input".to_owned()); + } + if row.trim().is_empty() { + break; + } + matrix.push(row.trim().chars().collect()); + } + if matrix + .iter() + .any(|row| row.len() != matrix.first().unwrap().len()) + { + return Err("All rows must have the same length".to_owned()); + } + Ok(matrix) +} + +fn main() { + println!("Enter the matrix line by line without spaces (empty line to finish)"); + println!("Use # for blocks and - for empty spaces"); + let matrix = match read_matrix() { + Ok(matrix) => matrix, + Err(e) => { + eprintln!("Error: {}", e); + exit(1); + } + }; + let result = match switch_gravity_on(&matrix) { + Some(matrix) => matrix, + None => { + eprintln!("Error: Invalid input"); + exit(1); + } + }; + println!("{:?}", result); +} diff --git a/Week-08/Day-52_Switch-On-The-Gravity/day52/tests/examples.rs b/Week-08/Day-52_Switch-On-The-Gravity/day52/tests/examples.rs new file mode 100644 index 0000000..ff0252f --- /dev/null +++ b/Week-08/Day-52_Switch-On-The-Gravity/day52/tests/examples.rs @@ -0,0 +1,53 @@ +#[cfg(test)] +mod examples { + use day52::switch_gravity_on; + + #[test] + fn example1() { + let matrix = vec![ + vec!['-', '#', '#', '-'], + vec!['-', '-', '-', '-'], + vec!['-', '-', '-', '-'], + vec!['-', '-', '-', '-'], + ]; + let expected = vec![ + vec!['-', '-', '-', '-'], + vec!['-', '-', '-', '-'], + vec!['-', '-', '-', '-'], + vec!['-', '#', '#', '-'], + ]; + assert_eq!(switch_gravity_on(&matrix), Some(expected)); + } + + #[test] + fn example2() { + let matrix = vec![ + vec!['-', '#', '#', '-'], + vec!['-', '-', '#', '-'], + vec!['-', '-', '-', '-'], + ]; + let expected = vec![ + vec!['-', '-', '-', '-'], + vec!['-', '-', '#', '-'], + vec!['-', '#', '#', '-'], + ]; + assert_eq!(switch_gravity_on(&matrix), Some(expected)); + } + + #[test] + fn example3() { + let matrix = vec![ + vec!['-', '#', '#', '#', '#', '-'], + vec!['#', '-', '-', '#', '#', '-'], + vec!['-', '#', '-', '-', '-', '-'], + vec!['-', '-', '-', '-', '-', '-'], + ]; + let expected = vec![ + vec!['-', '-', '-', '-', '-', '-'], + vec!['-', '-', '-', '-', '-', '-'], + vec!['-', '#', '-', '#', '#', '-'], + vec!['#', '#', '#', '#', '#', '-'], + ]; + assert_eq!(switch_gravity_on(&matrix), Some(expected)); + } +}