Skip to content

Commit

Permalink
Day 3: Mull It Over
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 3, 2024
1 parent f9a3caf commit 3aae14b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Development occurs in language-specific directories:
|--:|--:|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|[day1.rs](rs/src/day1.rs)|
|[Day2.hs](hs/src/Day2.hs)|[Day2.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day2.kt)|[day2.py](py/aoc2024/day2.py)|[day2.rs](rs/src/day2.rs)|
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt)|[day3.py](py/aoc2024/day3.py)||
|[Day3.hs](hs/src/Day3.hs)|[Day3.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day3.kt)|[day3.py](py/aoc2024/day3.py)|[day3.rs](rs/src/day3.rs)|
9 changes: 8 additions & 1 deletion rs/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 rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ name = "aoc2024"
path = "src/main.rs"

[dependencies]
if_chain = "1.0.2"
itertools = "0.13"

[dev-dependencies]
Expand Down
8 changes: 7 additions & 1 deletion rs/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2024::{day1, day2};
use aoc2024::{day1, day2, day3};
use criterion::{black_box, Criterion};
use std::env;
use std::fs;
Expand Down Expand Up @@ -26,6 +26,12 @@ fn aoc2024_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day2::part2(black_box(&data))));
g.finish();

let data = get_day_input(3)?;
let mut g = c.benchmark_group("day 3");
g.bench_function("part 1", |b| b.iter(|| day3::part1(black_box(&data))));
g.bench_function("part 2", |b| b.iter(|| day3::part2(black_box(&data))));
g.finish();

Ok(())
}

Expand Down
60 changes: 60 additions & 0 deletions rs/src/day3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use if_chain::if_chain;

pub fn part1(data: &str) -> i32 {
const PREFIX: &str = "mul(";

let mut data = data;
let mut total = 0;
while let Some(offset) = data.find(PREFIX) {
data = data[offset..].strip_prefix(PREFIX).unwrap();
if_chain! {
if let Some((a, b)) = data.split_once(',');
if let Ok(a) = a.parse::<i32>();
if let Some((b, c)) = b.split_once(')');
if let Ok(b) = b.parse::<i32>();
then {
data = c;
total += a * b;
}
}
}
total
}

pub fn part2(data: &str) -> i32 {
let mut data = data;
let mut total = 0;
while !data.is_empty() {
let end = data.find("don't()").unwrap_or(data.len());
total += part1(&data[..end]);
let Some(start) = data[end..].find("do()") else {
break;
};
data = &data[end + start..];
}
total
}

#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;

static EXAMPLE1: &str = indoc! {"
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
"};
static EXAMPLE2: &str = indoc! {"
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))
"};

#[test]
fn part1_examples() {
assert_eq!(161, part1(EXAMPLE1));
}

#[test]
fn part2_examples() {
assert_eq!(48, part2(EXAMPLE2));
}
}
1 change: 1 addition & 0 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod day1;
pub mod day2;
pub mod day3;
10 changes: 9 additions & 1 deletion rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2024::{day1, day2};
use aoc2024::{day1, day2, day3};
use std::collections::HashSet;
use std::env;
use std::fs;
Expand Down Expand Up @@ -32,5 +32,13 @@ fn main() -> io::Result<()> {
println!();
}

if args.is_empty() || args.contains("3") {
println!("Day 3");
let data = get_day_input(3)?;
println!("{:?}", day3::part1(&data));
println!("{:?}", day3::part2(&data));
println!();
}

Ok(())
}

0 comments on commit 3aae14b

Please sign in to comment.