Add 2024 Day 11
This commit is contained in:
parent
220483af05
commit
047100a358
5 changed files with 151 additions and 0 deletions
13
2024/day11_plutonian_pebbles/Cargo.toml
Normal file
13
2024/day11_plutonian_pebbles/Cargo.toml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
[package]
|
||||||
|
name = "day11_plutonian_pebbles"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
criterion = "0.5.1"
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "test_benchmark"
|
||||||
|
harness = false
|
74
2024/day11_plutonian_pebbles/challenge.md
Normal file
74
2024/day11_plutonian_pebbles/challenge.md
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
The ancient civilization on [Pluto](/2019/day/20) was known for its ability to manipulate spacetime, and while The Historians explore their infinite corridors, you've noticed a strange set of physics-defying stones.
|
||||||
|
|
||||||
|
At first glance, they seem like normal stones: they're arranged in a perfectly *straight line*, and each stone has a *number* engraved on it.
|
||||||
|
|
||||||
|
The strange part is that every time you blink, the stones *change*.
|
||||||
|
|
||||||
|
Sometimes, the number engraved on a stone changes. Other times, a stone might *split in two*, causing all the other stones to shift over a bit to make room in their perfectly straight line.
|
||||||
|
|
||||||
|
As you observe them for a while, you find that the stones have a consistent behavior. Every time you blink, the stones each *simultaneously* change according to the *first applicable rule* in this list:
|
||||||
|
|
||||||
|
* If the stone is engraved with the number `0`, it is replaced by a stone engraved with the number `1`.
|
||||||
|
* If the stone is engraved with a number that has an *even* number of digits, it is replaced by *two stones*. The left half of the digits are engraved on the new left stone, and the right half of the digits are engraved on the new right stone. (The new numbers don't keep extra leading zeroes: `1000` would become stones `10` and `0`.)
|
||||||
|
* If none of the other rules apply, the stone is replaced by a new stone; the old stone's number *multiplied by 2024* is engraved on the new stone.
|
||||||
|
|
||||||
|
No matter how the stones change, their *order is preserved*, and they stay on their perfectly straight line.
|
||||||
|
|
||||||
|
How will the stones evolve if you keep blinking at them? You take a note of the number engraved on each stone in the line (your puzzle input).
|
||||||
|
|
||||||
|
If you have an arrangement of five stones engraved with the numbers `0 1 10 99 999` and you blink once, the stones transform as follows:
|
||||||
|
|
||||||
|
* The first stone, `0`, becomes a stone marked `1`.
|
||||||
|
* The second stone, `1`, is multiplied by 2024 to become `2024`.
|
||||||
|
* The third stone, `10`, is split into a stone marked `1` followed by a stone marked `0`.
|
||||||
|
* The fourth stone, `99`, is split into two stones marked `9`.
|
||||||
|
* The fifth stone, `999`, is replaced by a stone marked `2021976`.
|
||||||
|
|
||||||
|
So, after blinking once, your five stones would become an arrangement of seven stones engraved with the numbers `1 2024 1 0 9 9 2021976`.
|
||||||
|
|
||||||
|
Here is a longer example:
|
||||||
|
|
||||||
|
```
|
||||||
|
Initial arrangement:
|
||||||
|
125 17
|
||||||
|
|
||||||
|
After 1 blink:
|
||||||
|
253000 1 7
|
||||||
|
|
||||||
|
After 2 blinks:
|
||||||
|
253 0 2024 14168
|
||||||
|
|
||||||
|
After 3 blinks:
|
||||||
|
512072 1 20 24 28676032
|
||||||
|
|
||||||
|
After 4 blinks:
|
||||||
|
512 72 2024 2 0 2 4 2867 6032
|
||||||
|
|
||||||
|
After 5 blinks:
|
||||||
|
1036288 7 2 20 24 4048 1 4048 8096 28 67 60 32
|
||||||
|
|
||||||
|
After 6 blinks:
|
||||||
|
2097446912 14168 4048 2 0 2 4 40 48 2024 40 48 80 96 2 8 6 7 6 0 3 2
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
In this example, after blinking six times, you would have `22` stones. After blinking 25 times, you would have `*55312*` stones!
|
||||||
|
|
||||||
|
Consider the arrangement of stones in front of you. *How many stones will you have after blinking 25 times?*
|
||||||
|
|
||||||
|
Your puzzle answer was `204022`.
|
||||||
|
|
||||||
|
\--- Part Two ---
|
||||||
|
----------
|
||||||
|
|
||||||
|
The Historians sure are taking a long time. To be fair, the infinite corridors *are* very large.
|
||||||
|
|
||||||
|
*How many stones would you have after blinking a total of 75 times?*
|
||||||
|
|
||||||
|
Your puzzle answer was `241651071960597`.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||||
|
|
||||||
|
At this point, you should [return to your Advent calendar](/2024) and try another puzzle.
|
||||||
|
|
||||||
|
If you still want to see it, you can [get your puzzle input](11/input).
|
62
2024/day11_plutonian_pebbles/src/lib.rs
Normal file
62
2024/day11_plutonian_pebbles/src/lib.rs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
use std::{collections::HashMap, num::ParseIntError};
|
||||||
|
|
||||||
|
|
||||||
|
/// Returns the number of stones that this `stone` will be replaced by after `remaining_steps`
|
||||||
|
/// blinks. We can iterate stone by stone, since they don't influence each other. `mem` is used for
|
||||||
|
/// memoization, since we expect many duplicate stones.
|
||||||
|
fn blink(stone: usize, remaining_steps: usize, mem: &mut HashMap<(usize, usize), usize>) -> usize {
|
||||||
|
if let Some(len) = mem.get(&(stone, remaining_steps)) {
|
||||||
|
return *len;
|
||||||
|
}
|
||||||
|
if remaining_steps == 0 {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// Rules:
|
||||||
|
// 0 -> 1
|
||||||
|
// ab -> a, b
|
||||||
|
// x -> 2024 * x
|
||||||
|
let next = if stone == 0 {
|
||||||
|
vec![1]
|
||||||
|
} else {
|
||||||
|
let stone_digits = stone.ilog10() + 1;
|
||||||
|
if stone_digits & 1 == 0 {
|
||||||
|
vec![stone / 10_usize.pow(stone_digits >> 1), stone % 10_usize.pow(stone_digits >> 1)]
|
||||||
|
} else {
|
||||||
|
// 2024 == 253 * 8. This is slightly faster than `stone * 2024`.
|
||||||
|
vec![(stone * 253) << 3]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let res = next.iter().map(|&stone| blink(stone, remaining_steps-1, mem)).sum();
|
||||||
|
mem.insert((stone, remaining_steps), res);
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run(input: &str) -> Result<(usize, usize), ParseIntError> {
|
||||||
|
let stones: Vec<_> = input.split_whitespace().map(|n| n.parse::<usize>()).collect::<Result<Vec<usize>, _>>()?;
|
||||||
|
let mut mem = HashMap::new();
|
||||||
|
let first = stones.iter().map(|&stone| blink(stone, 25, &mut mem)).sum();
|
||||||
|
let second = stones.iter().map(|&stone| blink(stone, 75, &mut mem)).sum();
|
||||||
|
Ok((first, second))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
|
fn read_file(name: &str) -> String {
|
||||||
|
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sample() {
|
||||||
|
let sample_input = read_file("tests/sample_input");
|
||||||
|
assert_eq!(run(&sample_input), Ok((55312, 65601038650482)));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_challenge() {
|
||||||
|
let challenge_input = read_file("tests/challenge_input");
|
||||||
|
assert_eq!(run(&challenge_input), Ok((204022, 241651071960597)));
|
||||||
|
}
|
||||||
|
}
|
1
2024/day11_plutonian_pebbles/tests/challenge_input
Normal file
1
2024/day11_plutonian_pebbles/tests/challenge_input
Normal file
|
@ -0,0 +1 @@
|
||||||
|
0 37551 469 63 1 791606 2065 9983586
|
1
2024/day11_plutonian_pebbles/tests/sample_input
Normal file
1
2024/day11_plutonian_pebbles/tests/sample_input
Normal file
|
@ -0,0 +1 @@
|
||||||
|
125 17
|
Loading…
Add table
Add a link
Reference in a new issue