Day 08: Matchsticks
This one is pretty simple! For part 1 we can use regex to replace the escaped characters with their actual character (or a dummy character) and subtract 2 for the ending quotes. For part 2 we replace all the characters that need to be escaped with their escaped version and add 2 for the ending quotes.
Loading code...
use regex::Regex;
pub fn day_08_1 (input: &str) -> String {
return input.lines()
.map(|line| {
let code_length = line.len();
let line = Regex::new(r#"(\\\\|\\x..|\\")"#).unwrap().replace_all(&line, "x");
let parsed_length = line.len() - 2;
return (code_length, parsed_length);
})
.fold(0, |acc, e| acc + (e.0.abs_diff(e.1)))
.to_string();
}
pub fn day_08_2 (input: &str) -> String {
return input.lines()
.map(|line| {
let code_length = line.len();
let line = line.replace("\"", "\"\"").replace("\\", "\\\\");
let parsed_length = line.len() + 2;
return (code_length, parsed_length);
})
.fold(0, |acc, e| acc + (e.0.abs_diff(e.1)))
.to_string();
}