use std::env::args; use std::fs::File; use std::io::BufRead; use std::io::BufReader; fn parse_line(line: &str) -> Result, char> { let mut stack = vec![]; for c in line.chars() { match c { '(' | '[' | '{' | '<' => stack.push(c), ')' => { if stack.pop() != Some('(') { return Err(c); } }, ']' => { if stack.pop() != Some('[') { return Err(c); } }, '}' => { if stack.pop() != Some('{') { return Err(c); } }, '>' => { if stack.pop() != Some('<') { return Err(c); } }, _ => unreachable!(), } } return Ok(stack); } fn main() { let path = args().nth(1).unwrap(); let file = File::open(path).unwrap(); let mut score = 0; for line in BufReader::new(file).lines() { match parse_line(&line.unwrap()) { Ok(_) => {}, Err(')') => score += 3, Err(']') => score += 57, Err('}') => score += 1197, Err('>') => score += 25137, _ => unreachable!(), } } print!("{}\n", score); }