aoc2024/src/solutions/day_3.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

2024-12-05 17:55:56 +00:00
use std::{fs::read_to_string, path::Path, sync::LazyLock};
2024-12-03 11:05:54 +00:00
use regex::Regex;
use super::AocSolution;
2024-12-05 17:55:56 +00:00
use crate::utils::Result;
2024-12-03 11:05:54 +00:00
static MUL: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"mul\([0-9]{1,3},[0-9]{1,3}\)").unwrap());
static COND_MUL: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(mul\([0-9]{1,3},[0-9]{1,3}\))|(do\(\))|(don't\(\))").unwrap());
fn as_mul(s: &str) -> u64 {
let (a, b) = s[4..(s.len() - 1)]
.split_once(',')
.map(|(a, b)| (a.parse::<u64>().unwrap(), b.parse::<u64>().unwrap()))
.unwrap();
a * b
}
2024-12-03 11:13:40 +00:00
#[derive(Clone, Copy)]
pub struct AocDayThreeSolution;
2024-12-03 11:05:54 +00:00
impl AocSolution for AocDayThreeSolution {
type Output = u64;
2024-12-05 17:55:56 +00:00
fn get_input(&self, path: Option<&Path>) -> Result<String> {
Ok(match path {
Some(p) => read_to_string(p)?,
None => read_to_string("./input/day_3.txt")?,
})
}
fn part_one(&self, input: &str) -> Result<Self::Output> {
Ok(MUL.find_iter(input).map(|x| x.as_str()).map(as_mul).sum())
2024-12-03 11:05:54 +00:00
}
2024-12-05 17:55:56 +00:00
fn part_two(&self, input: &str) -> Result<Self::Output> {
Ok(COND_MUL
.find_iter(input)
2024-12-03 11:05:54 +00:00
.map(|x| x.as_str())
.fold((0, true), |(sum, flag), cut| {
if cut.starts_with("mul") && flag {
(sum + as_mul(cut), flag)
} else if cut == "do()" {
(sum, true)
} else {
(sum, false)
}
})
2024-12-05 17:55:56 +00:00
.0)
2024-12-03 11:05:54 +00:00
}
}