Skip to content

Commit

Permalink
Solved day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
haphaeu committed Dec 11, 2024
1 parent 2932bf4 commit aa71aab
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 6 deletions.
99 changes: 93 additions & 6 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test_lib = []
chrono = { version = "0.4.38", optional = true }
dhat = { version = "0.3.3", optional = true }
itertools = "0.13.0"
memoize = "0.4.2"
pico-args = "0.5.0"
regex = "1.11.1"
tinyjson = "2.5.1"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ algorithm to Rust, and *when I find the time*, optimising that algorithm in Rust
| [Day 8](./src/bin/08.rs) | `-` | `-` |
| [Day 9](./src/bin/09.rs) | `-` | `-` |
| [Day 10](./src/bin/10.rs) | `547.0µs` | `432.9µs` |
| [Day 11](./src/bin/11.rs) | `272.0ns` | `259.0ns` |

**Total: 3538.78ms**
<!--- 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
62 changes: 62 additions & 0 deletions src/bin/11.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
advent_of_code::solution!(11);
use memoize::memoize;

fn parse_input(input: &str) -> Vec<u64> {
input
.trim()
.split_whitespace()
.map(|s| s.parse().unwrap())
.collect()
}

// Memoization here does the heck of a job...
#[memoize]
fn blink(stone: u64, n_blinks: u32) -> u64 {
if n_blinks == 0 {
return 1;
}
if stone == 0 {
return blink(1, n_blinks - 1);
}

let digits = stone.ilog10() as u32 + 1;
if digits % 2 == 0 {
let left = stone / 10_u64.pow(digits / 2);
let right = stone % 10_u64.pow(digits / 2);
return blink(left, n_blinks - 1) + blink(right, n_blinks - 1);
}

return blink(stone * 2024, n_blinks - 1);
}

fn blinks(input: &str, times: u32) -> u64 {
let stones = parse_input(input);
stones.iter().map(|&stone| blink(stone, times)).sum()
}

pub fn part_one(input: &str) -> Option<u64> {
Some(blinks(input, 25))
}

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

Please sign in to comment.