diff options
Diffstat (limited to '10/src/part-1.rs')
-rw-r--r-- | 10/src/part-1.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/10/src/part-1.rs b/10/src/part-1.rs new file mode 100644 index 0000000..c066310 --- /dev/null +++ b/10/src/part-1.rs @@ -0,0 +1,47 @@ +use std::io::{BufRead}; + +const SCORES: [usize; 4] = [3, 57, 1197, 25137]; + +fn num(c: char) -> usize { + match c { + '(' => 0, + '[' => 1, + '{' => 2, + '<' => 3, + ')' => 4, + ']' => 5, + '}' => 6, + '>' => 7, + _ => panic!("Unknown character: {}", c) + } +} + +pub fn main() { + let stdin = std::io::stdin(); + let handle = stdin.lock(); + + let mut score: usize = 0; + for l in handle.lines() { + let line = l.unwrap(); + //println!("{}", line); + let mut opened: Vec<usize> = Vec::new(); + for c in line.chars() { + let i = num(c); + if i < 4 { // Open. + opened.push(i); + } + else { // Close. + let last_opened = opened.pop().unwrap_or(usize::MAX); + if i-4 == last_opened { // Valid + } + else { // Invalid. + score += SCORES[i-4]; + //println!("Illegal {}.", c); + break; + } + } + } + } + + println!("Total score: {}", score); +} |