122 lines
4.0 KiB
Rust
122 lines
4.0 KiB
Rust
use std::fs::File;
|
|
use std::io::{BufReader};
|
|
use std::io::prelude::*;
|
|
|
|
|
|
fn main(){
|
|
let file = File::open("./input.txt").expect("Read failed");
|
|
let reader = BufReader::new(file);
|
|
|
|
let mut line_vec: Vec<Vec<char>> = Vec::new();
|
|
|
|
for line in reader.lines(){
|
|
let line_as_string = line.unwrap();
|
|
let line_as_char_vec = line_as_string.chars().collect::<Vec<char>>();
|
|
line_vec.push(line_as_char_vec);
|
|
}
|
|
|
|
let mut round_depth: u32 = 0;
|
|
let mut square_depth: u32 = 0;
|
|
let mut curly_depth: u32 = 0;
|
|
let mut pointy_depth: u32 = 0;
|
|
let mut current_opened: Vec<char> = Vec::new();
|
|
let mut corrput: bool = false;
|
|
let mut error_scores_2: Vec<u64> = Vec::new();
|
|
|
|
let mut illegal_chars: Vec<char> = Vec::new();
|
|
|
|
for (index, line) in line_vec.iter().enumerate() {
|
|
current_opened = Vec::new();
|
|
for symbol in line {
|
|
match symbol {
|
|
'(' => {round_depth += 1; current_opened.push('(');},
|
|
'[' => {square_depth += 1; current_opened.push('[');},
|
|
'{' => {curly_depth += 1; current_opened .push('{');},
|
|
'<' => {pointy_depth += 1; current_opened.push('<');},
|
|
')' => {
|
|
if round_depth > 0 && current_opened[current_opened.len()-1] == '(' {
|
|
round_depth -= 1;
|
|
current_opened.pop();
|
|
} else {
|
|
corrput = true;
|
|
illegal_chars.push(symbol.to_owned());
|
|
break;
|
|
}
|
|
},
|
|
']' => {
|
|
if square_depth > 0 && current_opened[current_opened.len()-1] == '[' {
|
|
square_depth -= 1; current_opened.pop();
|
|
} else {
|
|
corrput = true;
|
|
illegal_chars.push(symbol.to_owned());
|
|
break;
|
|
}
|
|
},
|
|
'}' => {
|
|
if curly_depth > 0 && current_opened[current_opened.len()-1] == '{' {
|
|
curly_depth -= 1;
|
|
current_opened.pop();
|
|
} else {
|
|
corrput = true;
|
|
illegal_chars.push(symbol.to_owned());
|
|
break;
|
|
}
|
|
},
|
|
'>' => {
|
|
if pointy_depth > 0 && current_opened[current_opened.len()-1] == '<' {
|
|
pointy_depth -= 1;
|
|
current_opened.pop();
|
|
} else {
|
|
corrput = true; illegal_chars.push(symbol.to_owned());
|
|
break;
|
|
}
|
|
},
|
|
_ => println!("No")
|
|
}
|
|
}
|
|
if corrput {corrput = false}
|
|
else if round_depth == 0 && square_depth == 0 && curly_depth == 0 && pointy_depth == 0 {
|
|
|
|
} else {
|
|
round_depth = 0;
|
|
square_depth = 0;
|
|
curly_depth = 0;
|
|
pointy_depth = 0;
|
|
|
|
let mut score: u64 = 0;
|
|
for i in 0..current_opened.len() {
|
|
|
|
let symbol = current_opened[current_opened.len()-1 - i];
|
|
score = score * 5;
|
|
match symbol {
|
|
'(' => score += 1,
|
|
'[' => score += 2,
|
|
'{' => score += 3,
|
|
'<' => score += 4,
|
|
_ => println!("ERROR")
|
|
}
|
|
}
|
|
error_scores_2.push(score);
|
|
score = 0;
|
|
|
|
|
|
}
|
|
}
|
|
|
|
let mut error_score: u32 = 0;
|
|
for symbol in illegal_chars {
|
|
match symbol {
|
|
')' => error_score += 3,
|
|
']' => error_score += 57,
|
|
'}' => error_score += 1197,
|
|
'>' => error_score += 25137,
|
|
_ => println!("ERROR")
|
|
}
|
|
}
|
|
|
|
|
|
println!("Error score: {}", error_score);
|
|
error_scores_2.sort();
|
|
println!("Error scores 2: {}", error_scores_2[(error_scores_2.len()-1) /2 ] );
|
|
|
|
} |