Skip to content

Commit

Permalink
Merge pull request #86 from ephemient/rs/day11
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 11, 2024
2 parents f842c4e + 9873fde commit f3599f7
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ Development occurs in language-specific directories:
|[Day8.hs](hs/src/Day8.hs)|[Day8.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day8.kt)|[day8.py](py/aoc2024/day8.py)|[day8.rs](rs/src/day8.rs)|
|[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day9.kt)|[day9.py](py/aoc2024/day9.py)|[day9.rs](rs/src/day9.rs)|
|[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.kt)|[day10.py](py/aoc2024/day10.py)|[day10.rs](rs/src/day10.rs)|
|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)|[day11.py](py/aoc2024/day11.py)||
|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)|[day11.py](py/aoc2024/day11.py)|[day11.rs](rs/src/day11.rs)|
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, day10, day2, day3, day4, day5, day6, day7, day8, day9};
use aoc2024::{day1, day10, day11, day2, day3, day4, day5, day6, day7, day8, day9};
use criterion::{black_box, Criterion};
use std::env;
use std::fs;
Expand Down Expand Up @@ -74,6 +74,12 @@ fn aoc2024_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day10::part2(black_box(&data))));
g.finish();

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

Ok(())
}

Expand Down
74 changes: 74 additions & 0 deletions rs/src/day11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::collections::BTreeMap;
use std::num::ParseIntError;

pub fn part1(data: &str) -> Result<u64, ParseIntError> {
solve(data, 25)
}

pub fn part2(data: &str) -> Result<u64, ParseIntError> {
solve(data, 75)
}

fn solve(data: &str, n: usize) -> Result<u64, ParseIntError> {
Ok((0..n)
.fold(
{
let mut counts = BTreeMap::new();
for word in data.split_ascii_whitespace() {
counts
.entry(word.parse::<u64>()?)
.and_modify(|e| *e += 1)
.or_insert(1u64);
}
counts
},
|counts, _| {
let mut next = BTreeMap::new();
for (num, count) in counts {
if num == 0 {
next.entry(1).and_modify(|e| *e += count).or_insert(count);
} else {
let length = num.ilog10() + 1;
if length % 2 == 0 {
let divisor = 10u64.pow(length / 2);
next.entry(num / divisor)
.and_modify(|e| *e += count)
.or_insert(count);
next.entry(num % divisor)
.and_modify(|e| *e += count)
.or_insert(count);
} else {
next.entry(2024 * num)
.and_modify(|e| *e += count)
.or_insert(count);
}
}
}
next
},
)
.into_values()
.sum())
}

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

static EXAMPLE_1: &str = indoc! {"
0 1 10 99 999
"};

static EXAMPLE_2: &str = indoc! {"
125 17
"};

#[test]
fn part1_examples() {
assert_eq!(Ok(7), solve(EXAMPLE_1, 1));
assert_eq!(Ok(22), solve(EXAMPLE_2, 6));
assert_eq!(Ok(55312), solve(EXAMPLE_2, 25));
}
}
1 change: 1 addition & 0 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod day1;
pub mod day10;
pub mod day11;
pub mod day2;
pub mod day3;
pub mod day4;
Expand Down
10 changes: 9 additions & 1 deletion rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::anyhow;
use aoc2024::{day1, day10, day2, day3, day4, day5, day6, day7, day8, day9};
use aoc2024::{day1, day10, day11, day2, day3, day4, day5, day6, day7, day8, day9};
use std::collections::HashSet;
use std::env;
use std::fs;
Expand Down Expand Up @@ -97,5 +97,13 @@ fn main() -> anyhow::Result<()> {
println!();
}

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

Ok(())
}

0 comments on commit f3599f7

Please sign in to comment.