From 36ff2f2c3564641ee2e41fbe8a6b9cdb2d14005c Mon Sep 17 00:00:00 2001 From: Lucy Date: Fri, 13 Dec 2024 08:49:54 +0100 Subject: [PATCH] feat: day 11 --- Cargo.toml | 2 + inputs/day11/example.txt | 1 + src/day11.rs | 79 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 4 ++ 4 files changed, 86 insertions(+) create mode 100644 inputs/day11/example.txt create mode 100644 src/day11.rs diff --git a/Cargo.toml b/Cargo.toml index 0dd9a1a..7deb2fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ full = [ "day8", "day9", "day10", + "day11", ] default = ["full"] day1 = [] @@ -27,6 +28,7 @@ day7 = [] day8 = [] day9 = [] day10 = [] +day11 = [] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/inputs/day11/example.txt b/inputs/day11/example.txt new file mode 100644 index 0000000..528f9d5 --- /dev/null +++ b/inputs/day11/example.txt @@ -0,0 +1 @@ +125 17 \ No newline at end of file diff --git a/src/day11.rs b/src/day11.rs new file mode 100644 index 0000000..f15e112 --- /dev/null +++ b/src/day11.rs @@ -0,0 +1,79 @@ +#![allow( + clippy::cast_sign_loss, + clippy::cast_possible_wrap, + clippy::too_many_lines +)] + +use std::collections::HashMap; + +use tracing::debug; + +fn blink_v2(stones: HashMap) -> HashMap { + let mut res: HashMap = HashMap::new(); + for (stone, count) in stones { + match stone { + 0 => { + res.insert(1, *(res.get(&1).unwrap_or(&0)) + count); + } + s if s.to_string().len() % 2 == 0 => { + let s_string = s.to_string(); + let split = s_string.split_at(s_string.len() / 2); + let left_half = split.0.parse::().unwrap(); + res.insert(left_half, *(res.get(&left_half).unwrap_or(&0)) + count); + let right_half = split.1.parse::().unwrap(); + res.insert(right_half, *(res.get(&right_half).unwrap_or(&0)) + count); + } + v => { + let new_stone = v * 2024; + res.insert(new_stone, *(res.get(&new_stone).unwrap_or(&0)) + count); + } + } + } + res +} +pub fn pt1(input: &str) -> usize { + let mut stones: HashMap = HashMap::new(); + for stone in input + .split_whitespace() + .map(|s| s.parse::().unwrap()) + { + stones.insert(stone, stones.get(&stone).unwrap_or(&0) + 1); + } + for step in 0..25 { + debug!("step {step}"); + stones = blink_v2(stones); + } + + stones.values().sum() +} +pub fn pt2(input: &str) -> usize { + let mut stones: HashMap = HashMap::new(); + for stone in input + .split_whitespace() + .map(|s| s.parse::().unwrap()) + { + stones.insert(stone, stones.get(&stone).unwrap_or(&0) + 1); + } + for step in 0..75 { + debug!("step {step}"); + stones = blink_v2(stones); + } + + stones.values().sum() +} + +#[cfg(test)] +mod tests { + use tracing_test::traced_test; + + use super::pt1; + use std::fs::read_to_string; + + #[traced_test] + #[test] + pub fn test_pt1() { + let input = + read_to_string("./inputs/day11/example.txt").expect("Missing example.txt for day11"); + assert_eq!(pt1(&input), 55312) + } +} diff --git a/src/main.rs b/src/main.rs index 7311ed5..e373132 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,8 @@ mod day08; mod day09; #[cfg(feature = "day10")] mod day10; +#[cfg(feature = "day11")] +mod day11; fn main() -> eyre::Result<()> { color_eyre::install()?; @@ -49,6 +51,8 @@ fn main() -> eyre::Result<()> { solve_day(9, day09::pt1, day09::pt2); #[cfg(feature = "day10")] solve_day(10, day10::pt1, day10::pt2); + #[cfg(feature = "day11")] + solve_day(11, day11::pt1, day11::pt2); Ok(()) }