1
0

feat: solve day 10 part 2

This commit is contained in:
Rokas Puzonas 2021-12-10 15:38:39 +02:00
parent c7498748c7
commit 3a18776a44
2 changed files with 68 additions and 8 deletions

View File

@ -15,17 +15,22 @@ fn is_opening(c: char) -> bool {
c == '(' || c == '[' || c == '{' || c == '<'
}
// I know that it is not ideal to throw a panic here. But I'm too lazy to do
// error handling. And the input is guaranteed to be correct to this is fine.
fn get_chunk_variant(c: char) -> ChunkVariant {
match c {
'('|')' => ChunkVariant::Parenthesis,
'['|']' => ChunkVariant::Bracket,
'{'|'}' => ChunkVariant::Curly,
'<'|'>' => ChunkVariant::Pointy,
_ => panic!("Invalid chunk character")
}
}
fn find_corrupted_chunk_symbol(line: &str) -> Option<ChunkVariant> {
let mut active_chunks: Vec<ChunkVariant> = Vec::new();
for c in line.chars() {
let variant = match c {
'('|')' => ChunkVariant::Parenthesis,
'['|']' => ChunkVariant::Bracket,
'{'|'}' => ChunkVariant::Curly,
'<'|'>' => ChunkVariant::Pointy,
_ => panic!("Invalid character found while finding corrupted chunks")
};
let variant = get_chunk_variant(c);
if is_opening(c) {
active_chunks.push(variant);
} else {
@ -53,6 +58,42 @@ pub fn part1(lines: &Vec<String>) -> u32 {
return score;
}
fn find_unclosed_chunks(line: &str) -> Vec<ChunkVariant> {
let mut active_chunks: Vec<ChunkVariant> = Vec::new();
for c in line.chars() {
if is_opening(c) {
active_chunks.push(get_chunk_variant(c));
} else {
active_chunks.pop();
}
}
active_chunks
}
pub fn part2(lines: &Vec<String>) -> u64 {
let mut scores = Vec::new();
for line in lines {
let corrupted = find_corrupted_chunk_symbol(line);
if corrupted == None {
let mut score = 0;
let mut unclosed_chunks = find_unclosed_chunks(line);
unclosed_chunks.reverse();
for chunk in unclosed_chunks {
score *= 5;
score += match chunk {
ChunkVariant::Parenthesis => 1,
ChunkVariant::Bracket => 2,
ChunkVariant::Curly => 3,
ChunkVariant::Pointy => 4
}
}
scores.push(score);
}
}
scores.sort();
return scores[scores.len()/2];
}
#[cfg(test)]
mod tests {
use super::*;
@ -74,5 +115,23 @@ mod tests {
let result = part1(&input);
assert_eq!(result, 26397);
}
#[test]
fn part2_example() {
let input = vec![
"[({(<(())[]>[[{[]{<()<>>".into(),
"[(()[<>])]({[<{<<[]>>(".into(),
"{([(<{}[<>[]}>{[]{[(<()>".into(),
"(((({<>}<{<{<>}{[]{[]{}".into(),
"[[<[([]))<([[{}[[()]]]".into(),
"[{[{({}]{}}([{[{{{}}([]".into(),
"{<[[]]>}<{[{[{[]{()[[[]".into(),
"[<(<(<(<{}))><([]([]()".into(),
"<{([([[(<>()){}]>(<<{{".into(),
"<{([{{}}[<[[[<>{}]]]>[]]".into(),
];
let result = part2(&input);
assert_eq!(result, 288957);
}
}

View File

@ -43,6 +43,7 @@ fn run(day: i32, part: i32, input_filename: &str) {
"9.1" => println!("{}", day9::part1(day9::parse_input(&contents))),
"9.2" => println!("{}", day9::part2(day9::parse_input(&contents))),
"10.1" => println!("{}", day10::part1(&day10::parse_input(&contents))),
"10.2" => println!("{}", day10::part2(&day10::parse_input(&contents))),
_ => println!("Day {} part {} not found", day, part)
}
}