diff --git a/README.md b/README.md index 62f0c01b..04039ed3 100644 --- a/README.md +++ b/README.md @@ -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)| diff --git a/rs/benches/criterion.rs b/rs/benches/criterion.rs index 5a66f55f..2304bc2c 100644 --- a/rs/benches/criterion.rs +++ b/rs/benches/criterion.rs @@ -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; @@ -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(()) } diff --git a/rs/src/day9.rs b/rs/src/day9.rs new file mode 100644 index 00000000..b1040bad --- /dev/null +++ b/rs/src/day9.rs @@ -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::>(), + ) + } else { + 0 + } +} + +pub fn part1(data: &str) -> i32 { + data.lines() + .map(|line| { + predict( + &line + .split_whitespace() + .filter_map(|s| s.parse::().ok()) + .collect::>(), + ) + }) + .sum() +} + +pub fn part2(data: &str) -> i32 { + data.lines() + .map(|line| { + predict( + &line + .split_whitespace() + .rev() + .filter_map(|s| s.parse::().ok()) + .collect::>(), + ) + }) + .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)); + } +} diff --git a/rs/src/lib.rs b/rs/src/lib.rs index 65fdb47f..b7940e51 100644 --- a/rs/src/lib.rs +++ b/rs/src/lib.rs @@ -6,3 +6,4 @@ pub mod day5; pub mod day6; pub mod day7; pub mod day8; +pub mod day9; diff --git a/rs/src/main.rs b/rs/src/main.rs index 46711120..f7c3c2bf 100644 --- a/rs/src/main.rs +++ b/rs/src/main.rs @@ -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; @@ -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(()) }