1
Fork 0

Solve day 10!

This commit is contained in:
Bauke 2021-12-10 13:37:49 +01:00
parent ad0f5d63a1
commit 0eaa4648f0
Signed by: Bauke
GPG Key ID: C1C0F29952BCF558
2 changed files with 93 additions and 0 deletions

91
source/day_10/mod.rs Normal file
View File

@ -0,0 +1,91 @@
use std::collections::VecDeque;
use color_eyre::Result;
pub fn solve() -> Result<()> {
let input_data = include_str!("../../data/day_10.txt").trim();
println!("Day 10 Part 1: {}", part_1(input_data)?);
println!("Day 10 Part 2: {}", part_2(input_data)?);
Ok(())
}
fn part_1(input: &str) -> Result<usize> {
let mut syntax_error_score = 0;
for line in input.lines() {
let mut tokens = VecDeque::new();
for token in line.chars() {
if ['(', '[', '{', '<'].contains(&token) {
tokens.push_back(token);
continue;
}
let expected_close = match tokens.pop_back() {
Some('(') => ')',
Some('[') => ']',
Some('{') => '}',
Some('<') => '>',
_ => unreachable!(),
};
if token != expected_close {
syntax_error_score += match token {
')' => 3,
']' => 57,
'}' => 1197,
'>' => 25137,
_ => unreachable!(),
};
}
}
}
Ok(syntax_error_score)
}
fn part_2(input: &str) -> Result<isize> {
let mut scores = vec![];
'line_loop: for line in input.lines() {
let mut line_score = 0;
let mut tokens = VecDeque::new();
for token in line.chars() {
if ['(', '[', '{', '<'].contains(&token) {
tokens.push_back(token);
continue;
}
let expected_close = match tokens.pop_back() {
Some('(') => ')',
Some('[') => ']',
Some('{') => '}',
Some('<') => '>',
_ => unreachable!(),
};
if token != expected_close {
continue 'line_loop;
}
}
for token in tokens.into_iter().rev() {
let token_score = match token {
'(' => 1,
'[' => 2,
'{' => 3,
'<' => 4,
_ => unreachable!(),
};
line_score *= 5;
line_score += token_score;
}
scores.push(line_score);
}
scores.sort_by(|a, b| a.cmp(b));
Ok(scores[scores.len() / 2])
}

View File

@ -11,6 +11,7 @@ mod day_06;
mod day_07; mod day_07;
mod day_08; mod day_08;
mod day_09; mod day_09;
mod day_10;
fn main() -> Result<()> { fn main() -> Result<()> {
color_eyre::install()?; color_eyre::install()?;
@ -27,6 +28,7 @@ fn main() -> Result<()> {
day_07::solve, day_07::solve,
day_08::solve, day_08::solve,
day_09::solve, day_09::solve,
day_10::solve,
]; ];
for day in days { for day in days {