Skip to content

Commit

Permalink
Solve day 3
Browse files Browse the repository at this point in the history
  • Loading branch information
caass committed Dec 9, 2024
1 parent 0806d94 commit c4e2649
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ either = "1.13.0"
memchr = "2.7.4"
pathsep = "0.1.1"
regex = "1.11.1"
void = "1.0.2"

[target.'cfg(windows)'.dependencies]
md-5 = "0.10.6"
Expand Down
79 changes: 79 additions & 0 deletions src/2024/03.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use eyre::Result;
use regex::Regex;
use void::Void;

use crate::common::{TryFromStr, TryParse};
use crate::meta::Problem;

pub const MULL_IT_OVER: Problem = Problem::solved(
&|input| input.try_parse::<CorruptedMemory>()?.sum_of_products(),
&|input| {
input
.try_parse::<CorruptedMemory>()?
.conditional_sum_of_products()
},
);

struct CorruptedMemory<'i>(&'i str);

impl<'i> TryFromStr<'i> for CorruptedMemory<'i> {
type Err = Void;

fn try_from_str(s: &'i str) -> Result<Self, Self::Err> {
Ok(Self(s))
}
}

thread_local! {
static MUL_RE: Regex = Regex::new(r"mul\((\d+),(\d+)\)").unwrap();
}

impl CorruptedMemory<'_> {
fn sum_of_products(&self) -> Result<usize> {
sum_products_in_str(self.0)
}

fn conditional_sum_of_products(&self) -> Result<usize> {
self.0
.split("do()")
.map(|chunk| {
chunk
.split_once("don't()")
.map_or(chunk, |(multiply_this, ..)| multiply_this)
})
.map(sum_products_in_str)
.try_fold(0, |a, res| res.map(|b| a + b))
}
}

fn sum_products_in_str(s: &str) -> Result<usize> {
MUL_RE.with(|re| {
re.captures_iter(s)
.map(|cap| -> Result<usize> {
let (_, [n_str, m_str]) = cap.extract();

let n = n_str.parse::<usize>()?;
let m = m_str.parse::<usize>()?;
Ok(n * m)
})
.try_fold(0usize, |a, res| res.map(|b| a + b))
})
}

#[test]
fn part_1() {
static INPUT: &str =
r"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";

let memory: CorruptedMemory = INPUT.try_parse().unwrap();
assert_eq!(memory.sum_of_products().unwrap(), 161);
}

#[test]
fn part_2() {
static INPUT: &str =
r"xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))";

let memory: CorruptedMemory = INPUT.try_parse().unwrap();
assert_eq!(memory.conditional_sum_of_products().unwrap(), 48);
}
3 changes: 2 additions & 1 deletion src/2024/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ use crate::meta::PROBLEMS;

PROBLEMS! {
01 => HISTORIAN_HYSTERIA,
02 => RED_NOSED_REPORTS
02 => RED_NOSED_REPORTS,
03 => MULL_IT_OVER
}
3 changes: 2 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ util::tests! {

2024: {
1: [2000468, 18567089],
2: [442]
2: [442],
3: [179834255, 80570939]
}
}

Expand Down

0 comments on commit c4e2649

Please sign in to comment.