From c709b79b2a6b3c91d04d2029b4e1e75af165769a Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sat, 14 Sep 2024 13:16:16 +0200 Subject: [PATCH] Wrote program for Day 51 --- README.md | 2 +- .../day51/Cargo.toml | 6 ++++ .../day51/src/lib.rs | 27 +++++++++++++++ .../day51/src/main.rs | 33 +++++++++++++++++++ .../day51/tests/examples.rs | 32 ++++++++++++++++++ 5 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 Week-08/Day-51_Asteroid-Collision/day51/Cargo.toml create mode 100644 Week-08/Day-51_Asteroid-Collision/day51/src/lib.rs create mode 100644 Week-08/Day-51_Asteroid-Collision/day51/src/main.rs create mode 100644 Week-08/Day-51_Asteroid-Collision/day51/tests/examples.rs diff --git a/README.md b/README.md index b7de00e..d55dbe0 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | 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_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_large_square: | +| 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 #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: | diff --git a/Week-08/Day-51_Asteroid-Collision/day51/Cargo.toml b/Week-08/Day-51_Asteroid-Collision/day51/Cargo.toml new file mode 100644 index 0000000..12a71a3 --- /dev/null +++ b/Week-08/Day-51_Asteroid-Collision/day51/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day51" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-08/Day-51_Asteroid-Collision/day51/src/lib.rs b/Week-08/Day-51_Asteroid-Collision/day51/src/lib.rs new file mode 100644 index 0000000..b636f90 --- /dev/null +++ b/Week-08/Day-51_Asteroid-Collision/day51/src/lib.rs @@ -0,0 +1,27 @@ +pub fn asteroid_collision(asteroids: &[isize]) -> Vec { + let mut stack = Vec::new(); + for &asteroid in asteroids { + if asteroid > 0 { + stack.push(asteroid); + } else { + if stack.is_empty() { + stack.push(asteroid); + continue; + } + while let Some(&top) = stack.last() { + if top < 0 { + stack.push(asteroid); + break; + } else if top == -asteroid { + stack.pop(); + break; + } else if top > -asteroid { + break; + } else { + stack.pop(); + } + } + } + } + stack +} diff --git a/Week-08/Day-51_Asteroid-Collision/day51/src/main.rs b/Week-08/Day-51_Asteroid-Collision/day51/src/main.rs new file mode 100644 index 0000000..00db0d1 --- /dev/null +++ b/Week-08/Day-51_Asteroid-Collision/day51/src/main.rs @@ -0,0 +1,33 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day51::asteroid_collision; + +fn read_isize_vec(request: &str) -> Result, Box> { + print!("{}", request); + io::stdout().flush()?; + let mut buffer = String::new(); + io::stdin().read_line(&mut buffer)?; + let isize_vec = buffer + .split_whitespace() + .map(|x| match x.parse() { + Ok(num) => Ok(num), + Err(err) => Err(format!("Error parsing '{}': {}", x, err)), + }) + .collect::, String>>()?; + Ok(isize_vec) +} + +fn main() { + let asteroids = match read_isize_vec("Enter the asteroids: ") { + Ok(asteroids) => asteroids, + Err(err) => { + eprintln!("Error reading asteroids: {}", err); + exit(1); + } + }; + let result = asteroid_collision(&asteroids); + println!("{:?}", result); +} diff --git a/Week-08/Day-51_Asteroid-Collision/day51/tests/examples.rs b/Week-08/Day-51_Asteroid-Collision/day51/tests/examples.rs new file mode 100644 index 0000000..48a94d4 --- /dev/null +++ b/Week-08/Day-51_Asteroid-Collision/day51/tests/examples.rs @@ -0,0 +1,32 @@ +#[cfg(test)] +mod examples { + use day51::asteroid_collision; + + #[test] + fn example1() { + let asteroids = &[5, 10, -5]; + let res = asteroid_collision(asteroids); + assert_eq!(res, vec![5, 10]); + } + + #[test] + fn example2() { + let asteroids = &[8, -8]; + let res = asteroid_collision(asteroids); + assert_eq!(res, vec![]); + } + + #[test] + fn example3() { + let asteroids = &[10, 2, -5]; + let res = asteroid_collision(asteroids); + assert_eq!(res, vec![10]); + } + + #[test] + fn example4() { + let asteroids = &[-2, -1, 1, 2]; + let res = asteroid_collision(asteroids); + assert_eq!(res, vec![-2, -1, 1, 2]); + } +}