20 - Valid Parentheses
This commit is contained in:
		
							
								
								
									
										1
									
								
								src/input/20_test1
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/20_test1
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
()
 | 
			
		||||
							
								
								
									
										1
									
								
								src/input/20_test2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/20_test2
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
()[]{}
 | 
			
		||||
							
								
								
									
										1
									
								
								src/input/20_test3
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								src/input/20_test3
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
(]
 | 
			
		||||
@@ -7,7 +7,10 @@ use solutions::*;
 | 
			
		||||
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
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![];
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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 {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										98
									
								
								src/solutions/valid_parentheses.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								src/solutions/valid_parentheses.rs
									
									
									
									
									
										Normal 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");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user