Skip to content

Commit

Permalink
Added solution y2023/ex09
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Dec 9, 2023
1 parent bf01bd3 commit c0fa681
Show file tree
Hide file tree
Showing 7 changed files with 568 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ members = [
"y2023/ex06",
"y2023/ex07",
"y2023/ex08",
"y2023/ex09",
]

[profile.bench]
Expand Down
16 changes: 16 additions & 0 deletions y2023/ex09/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "y2023ex09"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
nom = "7.1.3"

[[bench]]
name = "bench_y2023ex09"
harness = false

[dev-dependencies]
criterion = "0.5.1"
110 changes: 110 additions & 0 deletions y2023/ex09/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Day 9: Mirage Maintenance

[Check it out on adventofcode.com](https://adventofcode.com/2023/day/9)

## Part One

You ride the camel through the sandstorm and stop where the ghost's maps told you to stop. The sandstorm subsequently subsides, somehow seeing you standing at an _oasis_!

The camel goes to get some water and you stretch your neck. As you look up, you discover what must be yet another giant floating island, this one made of metal! That must be where the _parts to fix the sand machines_ come from.

There's even a [hang glider](https://en.wikipedia.org/wiki/Hang_gliding) partially buried in the sand here; once the sun rises and heats up the sand, you might be able to use the glider and the hot air to get all the way up to the metal island!

While you wait for the sun to rise, you admire the oasis hidden here in the middle of Desert Island. It must have a delicate ecosystem; you might as well take some ecological readings while you wait. Maybe you can report any environmental instabilities you find to someone so the oasis can be around for the next sandstorm-worn traveler.

You pull out your handy _Oasis And Sand Instability Sensor_ and analyze your surroundings. The OASIS produces a report of many values and how they are changing over time (your puzzle input). Each line in the report contains the _history_ of a single value. For example:

0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45


To best protect the oasis, your environmental report should include a _prediction of the next value_ in each history. To do this, start by making a new sequence from the _difference at each step_ of your history. If that sequence is _not_ all zeroes, repeat this process, using the sequence you just generated as the input sequence. Once all of the values in your latest sequence are zeroes, you can extrapolate what the next value of the original history should be.

In the above dataset, the first history is `0 3 6 9 12 15`. Because the values increase by `3` each step, the first sequence of differences that you generate will be `3 3 3 3 3`. Note that this sequence has one fewer value than the input sequence because at each step it considers two numbers from the input. Since these values aren't _all zero_, repeat the process: the values differ by `0` at each step, so the next sequence is `0 0 0 0`. This means you have enough information to extrapolate the history! Visually, these sequences can be arranged like this:

0 3 6 9 12 15
3 3 3 3 3
0 0 0 0


To extrapolate, start by adding a new zero to the end of your list of zeroes; because the zeroes represent differences between the two values above them, this also means there is now a placeholder in every sequence above it:

0 3 6 9 12 15 B
3 3 3 3 3 A
0 0 0 0 0


You can then start filling in placeholders from the bottom up. `A` needs to be the result of increasing `3` (the value to its left) by `0` (the value below it); this means `A` must be `_3_`:

0 3 6 9 12 15 B
3 3 3 3 3 3
0 0 0 0 0


Finally, you can fill in `B`, which needs to be the result of increasing `15` (the value to its left) by `3` (the value below it), or `_18_`:

0 3 6 9 12 15 18
3 3 3 3 3 3
0 0 0 0 0


So, the next value of the first history is `_18_`.

Finding all-zero differences for the second history requires an additional sequence:

1 3 6 10 15 21
2 3 4 5 6
1 1 1 1
0 0 0


Then, following the same process as before, work out the next value in each sequence from the bottom up:

1 3 6 10 15 21 28
2 3 4 5 6 7
1 1 1 1 1
0 0 0 0


So, the next value of the second history is `_28_`.

The third history requires even more sequences, but its next value can be found the same way:

10 13 16 21 30 45 68
3 3 5 9 15 23
0 2 4 6 8
2 2 2 2
0 0 0


So, the next value of the third history is `_68_`.

If you find the next value for each history in this example and add them together, you get `_114_`.

Analyze your OASIS report and extrapolate the next value for each history. _What is the sum of these extrapolated values?_

Your puzzle answer was `1731106378`.

## Part Two

Of course, it would be nice to have _even more history_ included in your report. Surely it's safe to just _extrapolate backwards_ as well, right?

For each history, repeat the process of finding differences until the sequence of differences is entirely zero. Then, rather than adding a zero to the end and filling in the next values of each previous sequence, you should instead add a zero to the _beginning_ of your sequence of zeroes, then fill in new _first_ values for each previous sequence.

In particular, here is what the third example history looks like when extrapolating back in time:

5 10 13 16 21 30 45
5 3 3 5 9 15
-2 0 2 4 6
2 2 2 2
0 0 0


Adding the new values on the left side of each sequence from bottom to top eventually reveals the new left-most history value: `_5_`.

Doing this for the remaining example data above results in previous values of `_-3_` for the first history and `_0_` for the second history. Adding all three new values together produces `_2_`.

Analyze your OASIS report again, this time extrapolating the _previous_ value for each history. _What is the sum of these extrapolated values?_

Your puzzle answer was `1087`.
11 changes: 11 additions & 0 deletions y2023/ex09/benches/bench_y2023ex09.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use y2023ex09::{part1, part2};

fn criterion_benchmark(c: &mut Criterion) {
let input = include_str!("../input.txt");
c.bench_function("y2023ex09::part1", |b| b.iter(|| part1(black_box(input))));
c.bench_function("y2023ex09::part2", |b| b.iter(|| part2(black_box(input))));
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit c0fa681

Please sign in to comment.