feat(day3): init

This commit is contained in:
uku 2024-12-03 18:32:56 +01:00
parent f730b2225c
commit 06dc4402d1
Signed by: uku
SSH key fingerprint: SHA256:4P0aN6M8ajKukNi6aPOaX0LacanGYtlfjmN+m/sHY/o
4 changed files with 102 additions and 1 deletions

54
src/solutions/day_03.rs Normal file
View file

@ -0,0 +1,54 @@
use itertools::Itertools;
use regex::Regex;
use crate::common::{Answer, Solution};
pub struct Day03;
impl Solution for Day03 {
fn name(&self) -> &'static str {
"Mull It Over"
}
fn part_a(&self, input: &str) -> Answer {
let re = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
let sum = re
.captures_iter(input)
.map(|c| c.extract())
.map(|(_, [a, b])| a.parse::<u64>().unwrap() * b.parse::<u64>().unwrap())
.sum::<u64>();
Answer::Number(sum)
}
fn part_b(&self, input: &str) -> Answer {
let without_donts = input
.split("do()")
.map(|s| s.split_once("don't()").map(|(b, _)| b).unwrap_or(s))
.join("");
self.part_a(&without_donts)
}
}
#[cfg(test)]
mod test {
use super::Day03;
use crate::common::Solution;
const INPUT_A: &str = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))";
const INPUT_B: &str =
"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";
#[test]
fn part_a() {
assert_eq!(Day03.part_a(INPUT_A), 161.into());
}
#[test]
fn part_b() {
assert_eq!(Day03.part_b(INPUT_B), 48.into());
}
}

View file

@ -2,5 +2,6 @@ use crate::common::Solution;
mod day_01;
mod day_02;
mod day_03;
pub const SOLUTIONS: &[&dyn Solution] = &[&day_01::Day01, &day_02::Day02];
pub const SOLUTIONS: &[&dyn Solution] = &[&day_01::Day01, &day_02::Day02, &day_03::Day03];