Compare commits
2 Commits
2febd8de67
...
c967ea8a12
Author | SHA1 | Date | |
---|---|---|---|
c967ea8a12 | |||
c709b79b2a |
@ -97,8 +97,8 @@ 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 #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 #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_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: |
|
||||
|
6
Week-08/Day-51_Asteroid-Collision/day51/Cargo.toml
Normal file
6
Week-08/Day-51_Asteroid-Collision/day51/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day51"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
27
Week-08/Day-51_Asteroid-Collision/day51/src/lib.rs
Normal file
27
Week-08/Day-51_Asteroid-Collision/day51/src/lib.rs
Normal file
@ -0,0 +1,27 @@
|
||||
pub fn asteroid_collision(asteroids: &[isize]) -> Vec<isize> {
|
||||
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
|
||||
}
|
33
Week-08/Day-51_Asteroid-Collision/day51/src/main.rs
Normal file
33
Week-08/Day-51_Asteroid-Collision/day51/src/main.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use std::{
|
||||
io::{self, Write},
|
||||
process::exit,
|
||||
};
|
||||
|
||||
use day51::asteroid_collision;
|
||||
|
||||
fn read_isize_vec(request: &str) -> Result<Vec<isize>, Box<dyn std::error::Error>> {
|
||||
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::<Result<Vec<isize>, 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);
|
||||
}
|
32
Week-08/Day-51_Asteroid-Collision/day51/tests/examples.rs
Normal file
32
Week-08/Day-51_Asteroid-Collision/day51/tests/examples.rs
Normal file
@ -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]);
|
||||
}
|
||||
}
|
6
Week-08/Day-52_Switch-On-The-Gravity/day52/Cargo.toml
Normal file
6
Week-08/Day-52_Switch-On-The-Gravity/day52/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day52"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
22
Week-08/Day-52_Switch-On-The-Gravity/day52/src/lib.rs
Normal file
22
Week-08/Day-52_Switch-On-The-Gravity/day52/src/lib.rs
Normal file
@ -0,0 +1,22 @@
|
||||
pub fn switch_gravity_on(matrix: &Vec<Vec<char>>) -> Option<Vec<Vec<char>>> {
|
||||
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)
|
||||
}
|
45
Week-08/Day-52_Switch-On-The-Gravity/day52/src/main.rs
Normal file
45
Week-08/Day-52_Switch-On-The-Gravity/day52/src/main.rs
Normal file
@ -0,0 +1,45 @@
|
||||
use std::process::exit;
|
||||
|
||||
use day52::switch_gravity_on;
|
||||
|
||||
fn read_matrix() -> Result<Vec<Vec<char>>, String> {
|
||||
let mut matrix: Vec<Vec<char>> = 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);
|
||||
}
|
53
Week-08/Day-52_Switch-On-The-Gravity/day52/tests/examples.rs
Normal file
53
Week-08/Day-52_Switch-On-The-Gravity/day52/tests/examples.rs
Normal file
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user