1
0
Fork 0

Added Day 10 Part 2

This commit is contained in:
Ishan Jain 2021-12-10 11:58:11 +05:30
parent a1d1ff08c1
commit 67f7049ced
1 changed files with 27 additions and 19 deletions

View File

@ -11,43 +11,51 @@ fn parse_input(input: &'static str) -> impl Iterator<Item = &'static str> {
} }
fn solution(input: impl Iterator<Item = &'static str>) -> u64 { fn solution(input: impl Iterator<Item = &'static str>) -> u64 {
let mut score = 0; let mut candidates = vec![];
for line in input {
'outer: for line in input {
let mut stack = Vec::with_capacity(line.len()); let mut stack = Vec::with_capacity(line.len());
for c in line.chars() { for c in line.chars() {
match c { match c {
'{' | '(' | '<' | '[' => stack.push(c), '{' | '(' | '<' | '[' => stack.push(c),
'}' => { '}' | ']' | '>' => {
if stack.pop() != Some('{') { if stack.pop() != Some((c as u8 - 2) as char) {
score += 1197 continue 'outer;
} }
} }
')' => { ')' => {
if stack.pop() != Some('(') { if stack.pop() != Some('(') {
score += 3 continue 'outer;
}
}
'>' => {
if stack.pop() != Some('<') {
score += 25137
}
}
']' => {
if stack.pop() != Some('[') {
score += 57
} }
} }
_ => unreachable!(), _ => 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() { fn main() {