20 - Valid Parentheses

This commit is contained in:
Luke Else 2024-07-05 12:59:00 +01:00
parent 2318361bfc
commit 489960db0c
7 changed files with 111 additions and 1 deletions

5
.gitignore vendored
View File

@ -28,3 +28,8 @@ Cargo.lock
# Built Visual Studio Code Extensions # Built Visual Studio Code Extensions
*.vsix *.vsix
# Added by cargo
/target

1
src/input/20_test1 Normal file
View File

@ -0,0 +1 @@
()

1
src/input/20_test2 Normal file
View File

@ -0,0 +1 @@
()[]{}

1
src/input/20_test3 Normal file
View File

@ -0,0 +1 @@
(]

View File

@ -7,7 +7,10 @@ use solutions::*;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
let problems: Vec<Box<dyn Solution + Sync>> = vec![Box::new(xx::XX {})]; let problems: Vec<Box<dyn Solution + Sync>> = vec![
Box::new(xx::XX {}),
Box::new(valid_parentheses::ValidParentheses {})
];
let mut t = vec![]; let mut t = vec![];

View File

@ -1,6 +1,7 @@
use crate::utils::{self, get_input}; use crate::utils::{self, get_input};
use std::{error::Error, fmt::Display, time::SystemTime}; use std::{error::Error, fmt::Display, time::SystemTime};
pub mod valid_parentheses;
pub mod xx; pub mod xx;
pub trait Solution { pub trait Solution {

View File

@ -0,0 +1,98 @@
use super::Solution;
use std::{collections::HashMap, vec};
pub struct ValidParentheses {}
impl Solution for ValidParentheses {
fn solution(
&self,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
// Pairs of parentheses to match
let pairs = HashMap::from([(')', '('), (']', '['), ('}', '{')]);
let mut stack: Vec<char> = vec![];
let line = input.get(0).unwrap();
for char in line.chars() {
// Check if the new character is a closing parentheses
if pairs.contains_key(&char) {
// If it is, is the last character in the stack the correct opening one
// If not, false, otherwise continue
if stack.last() == None || stack.pop().unwrap() != *pairs.get(&char).unwrap() {
return Ok(Box::new(false));
}
} else {
stack.push(char)
}
}
// End stack will be empty if all parentheses are matched correctly
Ok(Box::new(stack.len() == 0))
}
fn get_id(&self) -> u8 {
20
}
}
impl ValidParentheses {}
/// Test from puzzle input
#[cfg(test)]
mod test {
use super::*;
use crate::*;
#[test]
fn test1() {
let solution = valid_parentheses::ValidParentheses {};
//Complete the Challenge
let answer = solution
.solution(
utils::get_input(solution.get_id(), utils::InputType::Test1)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "true");
}
#[test]
fn test2() {
let solution = valid_parentheses::ValidParentheses {};
//Complete the Challenge
let answer = solution
.solution(
utils::get_input(solution.get_id(), utils::InputType::Test2)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "true");
}
#[test]
fn test3() {
let solution = valid_parentheses::ValidParentheses {};
//Complete the Challenge
let answer = solution
.solution(
utils::get_input(solution.get_id(), utils::InputType::Test3)
.unwrap()
.as_mut(),
)
.unwrap()
.to_string();
assert_eq!(answer, "false");
}
}