Skip to content

Commit

Permalink
Day 11 part 2 done
Browse files Browse the repository at this point in the history
  • Loading branch information
javorszky committed Dec 12, 2024
1 parent 908d574 commit 8c842ae
Show file tree
Hide file tree
Showing 11 changed files with 408 additions and 6 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2021"

[workspace]
members = ["day01", "day02", "day03", "day04", "day05", "day06", "day07", "day08", "day09", "day10"]
members = ["day01", "day02", "day03", "day04", "day05", "day06", "day07", "day08", "day09", "day10", "day11"]
resolver = "2"

[dependencies]
Expand All @@ -19,3 +19,4 @@ day07 = {path = "day07"}
day08 = {path = "day08"}
day09 = {path = "day09"}
day10 = {path = "day10"}
day11 = {path = "day11"}
6 changes: 6 additions & 0 deletions day11/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "day11"
version = "0.1.0"
edition = "2021"

[dependencies]
1 change: 1 addition & 0 deletions day11/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17
1 change: 1 addition & 0 deletions day11/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 44 175060 3442 593 54398 9 8101095
60 changes: 60 additions & 0 deletions day11/part1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
--- Day 11: Plutonian Pebbles ---

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?

To begin, get your puzzle input.
5 changes: 5 additions & 0 deletions day11/part2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--- 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?**
20 changes: 20 additions & 0 deletions day11/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod part1;
mod part2;

pub fn solve_part1_example() -> usize {
let input = include_str!("../example.txt");

part1::solve(input)
}

pub fn solve_part1() -> usize {
let input = include_str!("../input.txt");

part1::solve(input)
}

pub fn solve_part2() -> u64 {
let input = include_str!("../input.txt");

part2::solve(input)
}
76 changes: 76 additions & 0 deletions day11/src/part1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#[derive(Debug, Clone, Eq, PartialEq)]
enum ParseError {
NotEvenDigit,
NotANumber
}

type Result<T> = std::result::Result<T, ParseError>;

pub(crate) fn blink(input: u64) -> Vec<u64> {
if input == 0 {
return vec![1]
}

if let Ok(nums) = half_number(input) {
return vec![
nums.0,
nums.1,
]
}

vec![input * 2024]
}

fn half_number(input: u64) -> Result<(u64, u64)> {
let number_as_string = format!("{}", input);
let l = number_as_string.len();
if l % 2 != 0 {
return Err(ParseError::NotEvenDigit)
}

let fh = &number_as_string[..l/2].parse::<u64>().or(Err(ParseError::NotANumber))?;
let sh = &number_as_string[l/2..].parse::<u64>().or(Err(ParseError::NotANumber))?;

Ok((*fh, *sh))
}

pub(crate) fn solve(input: &str) -> usize {
let mut numbers = input
.split_whitespace()
.map(|chunk| chunk.parse::<u64>().unwrap())
.collect::<Vec<u64>>();

// let mut precomputed: HashMap<u64, Vec<u64>> = HashMap::new();

for _i in 0..25 {
let mut new_numbers = Vec::new();
for num in numbers.iter() {
new_numbers.append(&mut blink(*num));
}

numbers = new_numbers;
}

numbers.len()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_half_number() {
assert_eq!(half_number(0), Err(ParseError::NotEvenDigit));
assert_eq!(half_number(10), Ok((1, 0)));
assert_eq!(half_number(1000), Ok((10, 0)));
assert_eq!(half_number(999), Err(ParseError::NotEvenDigit));
}

#[test]
fn test_blink() {
assert_eq!(blink(0), vec![1]);
assert_eq!(blink(1), vec![2024]);
assert_eq!(blink(11), vec![1, 1]);
assert_eq!(blink(2000), vec![20, 0]);
}
}
Loading

0 comments on commit 8c842ae

Please sign in to comment.