Added Day 10 Part 2
This commit is contained in:
parent
a1d1ff08c1
commit
67f7049ced
46
src/main.rs
46
src/main.rs
|
@ -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() {
|
||||||
|
|
Loading…
Reference in New Issue