add day 8 through 10

This commit is contained in:
timeshifter
2026-06-21 21:44:46 +02:00
parent 037c0d649c
commit 9adb5887bb
11 changed files with 647 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
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