Skip to content

Commit

Permalink
Day 9: Mirage Maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 9, 2023
1 parent 2330198 commit 6a540fc
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Development occurs in language-specific directories:
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day6.kt)|[day6.py](py/aoc2023/day6.py)|[day6.rs](rs/src/day6.rs)|
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day7.kt)|[day7.py](py/aoc2023/day7.py)|[day7.rs](rs/src/day7.rs)|
|[Day8.hs](hs/src/Day8.hs)|[Day8.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day8.kt)|[day8.py](py/aoc2023/day8.py)|[day8.rs](rs/src/day8.rs)|
|[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day9.kt)|[day9.py](py/aoc2023/day9.py)||
|[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day9.kt)|[day9.py](py/aoc2023/day9.py)|[day9.rs](rs/src/day9.rs)|
8 changes: 7 additions & 1 deletion rs/benches/criterion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2023::{day1, day2, day3, day4, day5, day6, day7, day8};
use aoc2023::{day1, day2, day3, day4, day5, day6, day7, day8, day9};
use criterion::{black_box, Criterion};
use std::env;
use std::fs;
Expand Down Expand Up @@ -62,6 +62,12 @@ fn aoc2023_bench(c: &mut Criterion) -> io::Result<()> {
g.bench_function("part 2", |b| b.iter(|| day8::part2(black_box(&data))));
g.finish();

let data = get_day_input(9)?;
let mut g = c.benchmark_group("day 9");
g.bench_function("part 1", |b| b.iter(|| day9::part1(black_box(&data))));
g.bench_function("part 2", |b| b.iter(|| day9::part2(black_box(&data))));
g.finish();

Ok(())
}

Expand Down
64 changes: 64 additions & 0 deletions rs/src/day9.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
fn predict(nums: &[i32]) -> i32 {
if nums.iter().any(|&x| x != 0) {
nums.last().unwrap()
+ predict(
&nums
.iter()
.zip(nums.iter().skip(1))
.map(|(x, y)| y - x)
.collect::<Vec<_>>(),
)
} else {
0
}
}

pub fn part1(data: &str) -> i32 {
data.lines()
.map(|line| {
predict(
&line
.split_whitespace()
.filter_map(|s| s.parse::<i32>().ok())
.collect::<Vec<_>>(),
)
})
.sum()
}

pub fn part2(data: &str) -> i32 {
data.lines()
.map(|line| {
predict(
&line
.split_whitespace()
.rev()
.filter_map(|s| s.parse::<i32>().ok())
.collect::<Vec<_>>(),
)
})
.sum()
}

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

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

#[test]
fn part1_examples() {
assert_eq!(114, part1(EXAMPLE));
}

#[test]
fn part2_examples() {
assert_eq!(2, part2(EXAMPLE));
}
}
1 change: 1 addition & 0 deletions rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod day5;
pub mod day6;
pub mod day7;
pub mod day8;
pub mod day9;
10 changes: 9 additions & 1 deletion rs/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use aoc2023::{day1, day2, day3, day4, day5, day6, day7, day8};
use aoc2023::{day1, day2, day3, day4, day5, day6, day7, day8, day9};
use std::collections::HashSet;
use std::env;
use std::fs;
Expand Down Expand Up @@ -80,5 +80,13 @@ fn main() -> io::Result<()> {
println!();
}

if args.is_empty() || args.contains("9") {
let data = get_day_input(9)?;
println!("Day 9");
println!("{:?}", day9::part1(&data));
println!("{:?}", day9::part2(&data));
println!();
}

Ok(())
}

0 comments on commit 6a540fc

Please sign in to comment.