Skip to content

Commit

Permalink
[2023] Complete day 9
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 9, 2023
1 parent 5bebbaa commit f3b1ad4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
- [Day 06: Wait For It](aoc_2023/src/day_06.rs)
- [Day 07: Camel Cards](aoc_2023/src/day_07.rs)
- [Day 08: Haunted Wasteland](aoc_2023/src/day_08.rs)
- [Day 09: Mirage Maintenance](aoc_2023/src/day_09.rs)
<!-- MARKER -->

## [2022](https://adventofcode.com/2022) [![aoc_2022](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml/badge.svg)](https://github.com/Basicprogrammer10/advent-of-code/actions/workflows/aoc_2022.yml)
Expand Down
98 changes: 98 additions & 0 deletions aoc_2023/src/day_09.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use common::{Answer, Solution};

pub struct Day09;

impl Solution for Day09 {
fn name(&self) -> &'static str {
"Mirage Maintenance"
}

fn part_a(&self, input: &str) -> Answer {
parse(input)
.iter()
.map(Sequence::predict)
.sum::<i64>()
.into()
}

fn part_b(&self, input: &str) -> Answer {
parse(input)
.iter()
.map(Sequence::predict_back)
.sum::<i64>()
.into()
}
}

struct Sequence {
values: Vec<i64>,
}

fn parse(input: &str) -> Vec<Sequence> {
let mut out = Vec::new();

for line in input.lines() {
let values = line
.split_whitespace()
.map(|v| v.parse().unwrap())
.collect();
out.push(Sequence { values });
}

out
}

impl Sequence {
fn derive(&self, rev: bool) -> Vec<Vec<i64>> {
let mut derived = vec![self.values.clone()];

while !derived.last().unwrap().iter().all(|&x| x == 0) {
let last = derived.last().unwrap();
let mut next = Vec::new();

for i in 1..last.len() {
if rev {
next.push(last[i - 1] - last[i]);
} else {
next.push(last[i] - last[i - 1]);
}
}

derived.push(next);
}

derived
}

fn predict(&self) -> i64 {
self.derive(false).iter().filter_map(|v| v.last()).sum()
}

fn predict_back(&self) -> i64 {
self.derive(true).iter().rev().map(|v| v[0]).sum()
}
}

#[cfg(test)]
mod test {
use common::Solution;
use indoc::indoc;

use super::Day09;

const CASE: &str = indoc! {"
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
"};

#[test]
fn part_a() {
assert_eq!(Day09.part_a(CASE), 114.into());
}

#[test]
fn part_b() {
assert_eq!(Day09.part_b(CASE), 2.into());
}
}
2 changes: 2 additions & 0 deletions aoc_2023/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod day_05;
mod day_06;
mod day_07;
mod day_08;
mod day_09;
// [import_marker]

#[rustfmt::skip]
Expand All @@ -22,5 +23,6 @@ pub const ALL: &[&dyn Solution] = &[
&day_06::Day06,
&day_07::Day07,
&day_08::Day08,
&day_09::Day09,
// [list_marker]
];

0 comments on commit f3b1ad4

Please sign in to comment.