50 lines
1.4 KiB
Rust
50 lines
1.4 KiB
Rust
use regex::Regex;
|
|
|
|
fn main() {
|
|
let content = std::fs::read_to_string("input.txt").unwrap();
|
|
let data: Vec<_> = content.lines().collect();
|
|
|
|
part1(&data);
|
|
part2(&data);
|
|
}
|
|
|
|
fn part2(data: &[&str]) {
|
|
let mut new_length = 0;
|
|
let mut old_length = 0;
|
|
|
|
let re_quote = Regex::new("\"").unwrap();
|
|
let re_backslash = Regex::new(r"\\").unwrap();
|
|
|
|
for line in data {
|
|
old_length += line.len();
|
|
let found = re_quote.replace_all(line, "AA");
|
|
let found = re_backslash.replace_all(found.as_ref(), "BB");
|
|
|
|
new_length += found.len() + 2; // add first and last quote
|
|
}
|
|
let result = new_length - old_length;
|
|
println!("{result}");
|
|
}
|
|
|
|
fn part1(data: &[&str]) {
|
|
let mut literal_length = 0;
|
|
let mut memory_length = 0;
|
|
|
|
let re_hex = Regex::new(r"\\x[a-f0-9]{2}").unwrap();
|
|
let re_backslash = Regex::new(r"\\\\").unwrap();
|
|
let re_doublequote = Regex::new(r"\x5c\x22").unwrap();
|
|
|
|
for line in data {
|
|
literal_length += line.len();
|
|
let found = re_hex.replace_all(line, "A");
|
|
let found = re_backslash.replace_all(found.as_ref(), "B");
|
|
let found = re_doublequote.replace_all(found.as_ref(), "C");
|
|
|
|
memory_length += found.len() - 2; // remove first and last quote
|
|
}
|
|
let result = literal_length - memory_length;
|
|
println!("{result}");
|
|
}
|
|
|
|
// https://adventofcode.com/2015/day/8
|