diff --git a/.gitignore b/.gitignore index cbf6334..1f3eb30 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,8 @@ Cargo.lock # Built Visual Studio Code Extensions *.vsix + + +# Added by cargo + +/target diff --git a/src/input/20_test1 b/src/input/20_test1 new file mode 100644 index 0000000..dd626a0 --- /dev/null +++ b/src/input/20_test1 @@ -0,0 +1 @@ +() \ No newline at end of file diff --git a/src/input/20_test2 b/src/input/20_test2 new file mode 100644 index 0000000..e11a44a --- /dev/null +++ b/src/input/20_test2 @@ -0,0 +1 @@ +()[]{} \ No newline at end of file diff --git a/src/input/20_test3 b/src/input/20_test3 new file mode 100644 index 0000000..fd8a840 --- /dev/null +++ b/src/input/20_test3 @@ -0,0 +1 @@ +(] \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4752654..99d5b1c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,10 @@ use solutions::*; #[tokio::main] async fn main() -> Result<(), Box> { - let problems: Vec> = vec![Box::new(xx::XX {})]; + let problems: Vec> = vec![ + Box::new(xx::XX {}), + Box::new(valid_parentheses::ValidParentheses {}) + ]; let mut t = vec![]; diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index 5cec3eb..5aa562e 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -1,6 +1,7 @@ use crate::utils::{self, get_input}; use std::{error::Error, fmt::Display, time::SystemTime}; +pub mod valid_parentheses; pub mod xx; pub trait Solution { diff --git a/src/solutions/valid_parentheses.rs b/src/solutions/valid_parentheses.rs new file mode 100644 index 0000000..d0c5ca1 --- /dev/null +++ b/src/solutions/valid_parentheses.rs @@ -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, + ) -> Result, Box> { + // Pairs of parentheses to match + let pairs = HashMap::from([(')', '('), (']', '['), ('}', '{')]); + + let mut stack: Vec = 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"); + } +}