diff --git a/src/bin/08.rs b/src/bin/08.rs new file mode 100644 index 000000000..3c989f0 --- /dev/null +++ b/src/bin/08.rs @@ -0,0 +1,51 @@ +use lazy_static::lazy_static; +use regex::Regex; + +lazy_static! { + static ref RE_MEM: Regex = Regex::new(r#"(\\\\|\\"|\\x.{2})"#).unwrap(); + static ref RE_ENCODE: Regex = Regex::new(r#"(\\|")"#).unwrap(); +} + +fn mem_size(line: &str) -> usize { + RE_MEM.replace_all(line, "").len() + RE_MEM.captures_iter(line).count() - 2 +} + +fn encode_size(line: &str) -> usize { + line.len() + RE_ENCODE.captures_iter(line).count() + 2 +} + +pub fn part_one(input: &str) -> Option { + let res: usize = input + .trim() + .split('\n') + .map(|x| x.len() - mem_size(x)) + .sum(); + Some(res as u32) +} +pub fn part_two(input: &str) -> Option { + let res: usize = input + .trim() + .split('\n') + .map(|x| encode_size(x) - x.len()) + .sum(); + Some(res as u32) +} +fn main() { + let input = &aoc::read_file("inputs", 8); + aoc::solve!(1, part_one, input); + aoc::solve!(2, part_two, input); +} +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_part_one() { + let input = aoc::read_file("test_inputs", 8); + assert_eq!(part_one(&input), Some(12)); + } + #[test] + fn test_part_two() { + let input = aoc::read_file("test_inputs", 8); + assert_eq!(part_two(&input), Some(19)); + } +} diff --git a/src/test_inputs/08.txt b/src/test_inputs/08.txt new file mode 100644 index 000000000..3d32fb3 --- /dev/null +++ b/src/test_inputs/08.txt @@ -0,0 +1,4 @@ +"" +"abc" +"aaa\"aaa" +"\x27"