solution: day8

This commit is contained in:
Matej Janezic 2022-12-04 17:24:52 +01:00
parent 00000150f0
commit 00000160da
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
2 changed files with 55 additions and 0 deletions

51
src/bin/08.rs Normal file
View File

@ -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<u32> {
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<u32> {
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));
}
}

4
src/test_inputs/08.txt Normal file
View File

@ -0,0 +1,4 @@
""
"abc"
"aaa\"aaa"
"\x27"