Solve day 11 of AoC 2024

This commit is contained in:
Ivan R. 2024-12-18 22:58:52 +05:00
parent 2c9b5c943e
commit fd021d5e14
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
6 changed files with 217 additions and 0 deletions

7
advent-of-code/2024/day_11/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day_11"
version = "0.1.0"

View file

@ -0,0 +1,6 @@
[package]
name = "day_11"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1,72 @@
# Advent of Code day 11 solution in Rust
## Plutonian Pebbles
[Task page](https://adventofcode.com/2024/day/11)
The ancient civilization on Pluto 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?
## 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?

View file

@ -0,0 +1 @@
4022724 951333 0 21633 5857 97 702 6

View file

@ -0,0 +1,107 @@
use std::collections::HashMap;
#[derive(Eq, PartialEq, Hash)]
struct Stone {
num: u64,
iterations_left: usize,
}
pub fn get_total_count(numbers: Vec<u64>, iterations: usize) -> usize {
let mut cache: HashMap<Stone, usize> = HashMap::new();
let mut count = 0;
for n in numbers {
count += get_stone_count(
Stone {
num: n,
iterations_left: iterations,
},
&mut cache,
);
}
count
}
fn get_stone_count(stone: Stone, cache: &mut HashMap<Stone, usize>) -> usize {
if stone.iterations_left == 0 {
return 1;
}
if let Some(res) = cache.get(&stone) {
return *res;
}
let mut total_count = 0;
for d in apply_rules(stone.num).iter() {
let new_stone = Stone {
num: *d,
iterations_left: stone.iterations_left - 1,
};
let count = get_stone_count(new_stone, cache);
total_count += count;
}
cache.insert(stone, total_count);
total_count
}
fn apply_rules(num: u64) -> Vec<u64> {
if num == 0 {
return vec![1];
}
let count = get_digit_count(num);
if count % 2 == 0 {
split_number(num, count)
} else {
vec![num * 2024]
}
}
fn get_digit_count(mut num: u64) -> usize {
let mut count = 0;
while num > 0 {
num /= 10;
count += 1;
}
count
}
fn split_number(mut num: u64, digit_count: usize) -> Vec<u64> {
let mut a = 0;
let mut b = 0;
let mut mult = 1;
for _i in 0..digit_count / 2 {
b += num % 10 * mult;
num /= 10;
mult *= 10;
}
mult = 1;
for _i in 0..digit_count / 2 {
a += num % 10 * mult;
num /= 10;
mult *= 10;
}
vec![a, b]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_total_count() {
assert_eq!(get_total_count(vec![125, 17], 6), 22);
}
#[test]
fn test_splitting() {
assert_eq!(split_number(2305, 4), vec![23, 5]);
assert_eq!(split_number(23, 2), vec![2, 3]);
assert_eq!(split_number(123040, 6), vec![123, 40]);
}
}

View file

@ -0,0 +1,24 @@
use day_11::get_total_count;
use std::error::Error;
use std::fs;
use std::io::Read;
fn main() -> Result<(), Box<dyn Error>> {
let mut file = fs::File::open("input.txt")?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
let input = String::from_utf8(buffer)?;
let numbers: Vec<u64> = input
.split_whitespace()
.map(|s| s.parse::<u64>().unwrap())
.collect();
let res1 = get_total_count(numbers.clone(), 25);
println!("Part 1: {}", res1);
let res2 = get_total_count(numbers, 75);
println!("Part 2: {}", res2);
Ok(())
}