Skip to content

Commit

Permalink
feat: day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefKuchar committed Dec 11, 2024
1 parent 0ea4e6b commit f03169f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
<!--- benchmarking table --->

---
Expand Down
1 change: 1 addition & 0 deletions data/examples/11.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17
58 changes: 58 additions & 0 deletions src/bin/11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::collections::HashMap;

advent_of_code::solution!(11);

pub fn solve(input: &str, iterations: usize) -> Option<u64> {
let mut stones: HashMap<u64, u64> = input
.split_whitespace()
.map(|x| (x.parse::<u64>().unwrap(), 1))
.collect();
for _ in 0..iterations {
let mut new_stones: HashMap<u64, u64> = 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::<u64>().unwrap(),
stone_str[(stone_str.len() / 2)..].parse::<u64>().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<u64> {
solve(input, 25)
}

pub fn part_two(input: &str) -> Option<u64> {
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));
}
}

0 comments on commit f03169f

Please sign in to comment.