summaryrefslogtreecommitdiff
path: root/10/src/part-2.rs
diff options
context:
space:
mode:
Diffstat (limited to '10/src/part-2.rs')
-rw-r--r--10/src/part-2.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/10/src/part-2.rs b/10/src/part-2.rs
new file mode 100644
index 0000000..76f8a2a
--- /dev/null
+++ b/10/src/part-2.rs
@@ -0,0 +1,57 @@
+use std::io::{BufRead};
+
+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 scores: Vec<usize> = Vec::new();
+ for l in handle.lines() {
+ let line = l.unwrap();
+ //println!("{}", line);
+ let mut corrupted = false;
+ 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.
+ corrupted = true;
+ break;
+ }
+ }
+ }
+
+ if !corrupted {
+ //println!("Processing uncorrupted, incomplete line. Got {} opened.", opened.len());
+ scores.push(0);
+ for i in opened.into_iter().rev() {
+ let last_score: &mut usize = scores.last_mut().unwrap();
+ *last_score *= 5;
+ *last_score += i + 1;
+ }
+ }
+ }
+
+ scores.sort();
+ assert!(scores.len() % 2 == 1);
+ println!("Score: {}", scores[scores.len() / 2]);
+}