From f03169f8744dada39e2c78692c853d5e6a97c9b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20Kucha=C5=99?= Date: Wed, 11 Dec 2024 10:30:49 +0100 Subject: [PATCH] feat: day 11 --- README.md | 3 ++- data/examples/11.txt | 1 + src/bin/11.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 data/examples/11.txt create mode 100644 src/bin/11.rs diff --git a/README.md b/README.md index 1b9e2b0..6fd4160 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 8](./src/bin/08.rs) | `55.8µs` | `136.5µs` | | [Day 9](./src/bin/09.rs) | `942.7µs` | `122.7ms` | | [Day 10](./src/bin/10.rs) | `530.9µs` | `284.2µs` | +| [Day 11](./src/bin/11.rs) | `238.7µs` | `12.7ms` | -**Total: 436.23ms** +**Total: 449.17ms** --- diff --git a/data/examples/11.txt b/data/examples/11.txt new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/data/examples/11.txt @@ -0,0 +1 @@ +125 17 diff --git a/src/bin/11.rs b/src/bin/11.rs new file mode 100644 index 0000000..769fd09 --- /dev/null +++ b/src/bin/11.rs @@ -0,0 +1,58 @@ +use std::collections::HashMap; + +advent_of_code::solution!(11); + +pub fn solve(input: &str, iterations: usize) -> Option { + let mut stones: HashMap = input + .split_whitespace() + .map(|x| (x.parse::().unwrap(), 1)) + .collect(); + for _ in 0..iterations { + let mut new_stones: HashMap = HashMap::new(); + for stone in stones.iter() { + let new = if *stone.0 == 0 { + vec![1] + } else { + let stone_str = stone.0.to_string(); + if stone_str.len() % 2 == 0 { + vec![ + stone_str[0..stone_str.len() / 2].parse::().unwrap(), + stone_str[(stone_str.len() / 2)..].parse::().unwrap(), + ] + } else { + vec![stone.0 * 2024] + } + }; + for n in new.iter() { + *new_stones.entry(*n).or_insert(0) += stone.1; + } + } + stones = new_stones; + } + Some(stones.iter().map(|x| x.1).sum()) +} + +pub fn part_one(input: &str) -> Option { + solve(input, 25) +} + +pub fn part_two(input: &str) -> Option { + solve(input, 75) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(55312)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(65601038650482)); + } +}