Skip to content

Commit

Permalink
2023-09: Solve second puzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
MLNW committed Dec 9, 2023
1 parent 583df3e commit 57bd3fd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
18 changes: 9 additions & 9 deletions 2023-rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.

| Day | Part 1 | Part 2 |
| :---: | :---: | :---: |
| [Day 1](./src/bin/01.rs) | `64.6µs` | `651.1µs` |
| [Day 2](./src/bin/02.rs) | `42.9µs` | `50.4µs` |
| [Day 3](./src/bin/03.rs) | `445.6µs` | `428.1µs` |
| [Day 4](./src/bin/04.rs) | `103.4µs` | `19.7ms` |
| [Day 1](./src/bin/01.rs) | `65.9µs` | `668.0µs` |
| [Day 2](./src/bin/02.rs) | `42.1µs` | `48.7µs` |
| [Day 3](./src/bin/03.rs) | `429.6µs` | `418.1µs` |
| [Day 4](./src/bin/04.rs) | `96.3µs` | `19.4ms` |
| [Day 5](./src/bin/05.rs) | `30.3µs` | `26.5s` |
| [Day 6](./src/bin/06.rs) | `642.0ns` | `37.2ms` |
| [Day 7](./src/bin/07.rs) | `205.1µs` | `-` |
| [Day 8](./src/bin/08.rs) | `257.5µs` | `1.7ms` |
| [Day 9](./src/bin/09.rs) | `101.1µs` | `-` |
| [Day 6](./src/bin/06.rs) | `573.0ns` | `40.4ms` |
| [Day 7](./src/bin/07.rs) | `200.0µs` | `-` |
| [Day 8](./src/bin/08.rs) | `256.2µs` | `1.7ms` |
| [Day 9](./src/bin/09.rs) | `106.4µs` | `110.1µs` |

**Total: 26560.95ms**
**Total: 26563.94ms**
<!--- benchmarking table --->

## Usage
Expand Down
39 changes: 30 additions & 9 deletions 2023-rust/src/bin/09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,41 @@ pub fn part_one(input: &str) -> Option<i32> {
Some(
sensor_readings
.iter()
.map(|readings| extrapolate(readings))
.map(|readings| extrapolate_forwards(readings))
.sum(),
)
}

pub fn part_two(input: &str) -> Option<u32> {
None
pub fn part_two(input: &str) -> Option<i32> {
let sensor_readings = parse_input(input);

Some(
sensor_readings
.iter()
.map(|readings| extrapolate_backwards(readings))
.sum(),
)
}

fn extrapolate(readings: &[i32]) -> i32 {
fn extrapolate_forwards(readings: &[i32]) -> i32 {
// Add the last row of differences together to extrapolate the next number
calc_differences(readings)
.iter()
.map(|differences| differences.last().unwrap())
.sum()
}

fn extrapolate_backwards(readings: &[i32]) -> i32 {
let differences = calc_differences(readings);
differences
.iter()
.map(|differences| differences.first().copied().unwrap())
.rev()
.reduce(|acc, number| number - acc)
.unwrap()
}

fn calc_differences(readings: &[i32]) -> Vec<Vec<i32>> {
let mut differences: Vec<Vec<i32>> = vec![readings.to_vec()];

let mut i = 0;
Expand All @@ -35,11 +60,7 @@ fn extrapolate(readings: &[i32]) -> i32 {
i += 1;
}

// Add the last row of differences together to extrapolate the next number
differences
.iter()
.map(|differences| differences.last().unwrap())
.sum()
}

fn parse_input(input: &str) -> Vec<Vec<i32>> {
Expand All @@ -66,6 +87,6 @@ mod tests {
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, None);
assert_eq!(result, Some(2));
}
}

0 comments on commit 57bd3fd

Please sign in to comment.