Wrote program for Day12
This commit is contained in:
parent
759f1bb3a1
commit
52289a98a2
@ -58,7 +58,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| Day #9 | [Trapping Rain Water](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-09_Trapping-Rain-Water) | :white_check_mark: |
|
| Day #9 | [Trapping Rain Water](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-09_Trapping-Rain-Water) | :white_check_mark: |
|
||||||
| Day #10 | [Unique Binary Search Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-10_Unique-Binary-Search-Trees) | :white_check_mark: |
|
| Day #10 | [Unique Binary Search Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-10_Unique-Binary-Search-Trees) | :white_check_mark: |
|
||||||
| Day #11 | [Restore IP Addresses](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-11_Restore-IP-Addresses) | :white_check_mark: |
|
| Day #11 | [Restore IP Addresses](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-11_Restore-IP-Addresses) | :white_check_mark: |
|
||||||
| Day #12 | [Mountains or Valleys](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-12_Mountains_And_Valleys) | :white_large_square: |
|
| Day #12 | [Mountains or Valleys](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-12_Mountains_And_Valleys) | :white_check_mark: |
|
||||||
| Day #13 | [Need Help With Your Packing](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-13_Need-Help-With-Packing) | :white_large_square: |
|
| Day #13 | [Need Help With Your Packing](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-13_Need-Help-With-Packing) | :white_large_square: |
|
||||||
| Day #14 | [The Karacas Encryption Algorithm](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-14_Karacas-Encryption-Algorithm) | :white_large_square: |
|
| Day #14 | [The Karacas Encryption Algorithm](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-14_Karacas-Encryption-Algorithm) | :white_large_square: |
|
||||||
| Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_large_square: |
|
| Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_large_square: |
|
||||||
|
6
Week-02/Day-12_Mountains_And_Valleys/day12/Cargo.toml
Normal file
6
Week-02/Day-12_Mountains_And_Valleys/day12/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day12"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
69
Week-02/Day-12_Mountains_And_Valleys/day12/src/lib.rs
Normal file
69
Week-02/Day-12_Mountains_And_Valleys/day12/src/lib.rs
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
pub enum LandscapeType {
|
||||||
|
Mountain,
|
||||||
|
Valley,
|
||||||
|
Neither,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_mountain(numbers: &[isize], max: (usize, &isize)) -> bool {
|
||||||
|
if max.0 > 0 && max.0 < numbers.len() - 1 {
|
||||||
|
for (i, n) in numbers[1..max.0].iter().enumerate() {
|
||||||
|
if numbers[i] > *n {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i, n) in numbers[1 + max.0..numbers.len() - 1].iter().enumerate() {
|
||||||
|
if *n > numbers[max.0 + i] {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_valley(numbers: &[isize], min: (usize, &isize)) -> bool {
|
||||||
|
if min.0 > 0 && min.0 < numbers.len() - 1 {
|
||||||
|
for (i, n) in numbers[1..min.0].iter().enumerate() {
|
||||||
|
if numbers[i] < *n {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (i, n) in numbers[1 + min.0..numbers.len()].iter().enumerate() {
|
||||||
|
if *n < numbers[min.0 + i] {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn landscape_type(numbers: &[isize]) -> LandscapeType {
|
||||||
|
if numbers.is_empty() {
|
||||||
|
return LandscapeType::Neither;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut min = (usize::MAX, &isize::MAX);
|
||||||
|
let mut max = (usize::MIN, &isize::MIN);
|
||||||
|
|
||||||
|
for (i, n) in numbers.iter().enumerate() {
|
||||||
|
if n < min.1 {
|
||||||
|
min = (i, n);
|
||||||
|
}
|
||||||
|
if n > max.1 {
|
||||||
|
max = (i, n);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_mountain(numbers, max) {
|
||||||
|
LandscapeType::Mountain
|
||||||
|
} else if is_valley(numbers, min) {
|
||||||
|
LandscapeType::Valley
|
||||||
|
} else {
|
||||||
|
LandscapeType::Neither
|
||||||
|
}
|
||||||
|
}
|
32
Week-02/Day-12_Mountains_And_Valleys/day12/src/main.rs
Normal file
32
Week-02/Day-12_Mountains_And_Valleys/day12/src/main.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
use day12::landscape_type;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
print!("Insert space separated numbers: ");
|
||||||
|
io::stdout().flush().expect("Failed to flush stdout.");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut buffer)
|
||||||
|
.expect("Failed to read from input");
|
||||||
|
|
||||||
|
let numbers: Vec<isize> = buffer
|
||||||
|
.trim()
|
||||||
|
.split(' ')
|
||||||
|
.filter(|x| !x.is_empty())
|
||||||
|
.map(|x| {
|
||||||
|
x.parse()
|
||||||
|
.expect("Error: Please enter numbers separated by spaces, e.g. 1 2 3")
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let result = match landscape_type(&numbers) {
|
||||||
|
day12::LandscapeType::Mountain => "Mountain",
|
||||||
|
day12::LandscapeType::Valley => "Valley",
|
||||||
|
day12::LandscapeType::Neither => "Neither",
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("{}", result);
|
||||||
|
}
|
30
Week-02/Day-12_Mountains_And_Valleys/day12/tests/examples.rs
Normal file
30
Week-02/Day-12_Mountains_And_Valleys/day12/tests/examples.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
use day12::{landscape_type, LandscapeType};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example1() {
|
||||||
|
assert!(matches!(
|
||||||
|
landscape_type(&[3, 4, 5, 4, 3]),
|
||||||
|
LandscapeType::Mountain
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example2() {
|
||||||
|
assert!(matches!(
|
||||||
|
landscape_type(&[9, 7, 3, 1, 2, 4]),
|
||||||
|
LandscapeType::Valley
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example3() {
|
||||||
|
assert!(matches!(landscape_type(&[9, 8, 9]), LandscapeType::Valley))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example4() {
|
||||||
|
assert!(matches!(
|
||||||
|
landscape_type(&[9, 8, 9, 8]),
|
||||||
|
LandscapeType::Neither
|
||||||
|
))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user