From 67f7049ced9a2970d98c8af02d53662acedfd446 Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Fri, 10 Dec 2021 11:58:11 +0530 Subject: [PATCH] Added Day 10 Part 2 --- src/main.rs | 46 +++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 84236cf..cb41c53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,43 +11,51 @@ fn parse_input(input: &'static str) -> impl Iterator { } fn solution(input: impl Iterator) -> u64 { - let mut score = 0; - for line in input { + let mut candidates = vec![]; + + 'outer: for line in input { let mut stack = Vec::with_capacity(line.len()); for c in line.chars() { match c { '{' | '(' | '<' | '[' => stack.push(c), - '}' => { - if stack.pop() != Some('{') { - score += 1197 + '}' | ']' | '>' => { + if stack.pop() != Some((c as u8 - 2) as char) { + continue 'outer; } } ')' => { if stack.pop() != Some('(') { - score += 3 - } - } - - '>' => { - if stack.pop() != Some('<') { - score += 25137 - } - } - - ']' => { - if stack.pop() != Some('[') { - score += 57 + continue 'outer; } } _ => unreachable!(), } } + + let mut score = 0; + while let Some(v) = stack.pop() { + score *= 5; + match v { + '(' => score += 1, + '[' => score += 2, + '{' => score += 3, + '<' => score += 4, + + _ => unreachable!(), + } + } + + candidates.push(score); } - score + let l = candidates.len(); + + candidates.select_nth_unstable(l / 2); + + candidates[candidates.len() / 2] } fn main() {