diff --git a/README.md b/README.md index 4a293fd..f277a54 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ | 6 | [src/solutions/day06.rs](src/solutions/day06.rs) | ⭐️⭐️ | | 7 | [src/solutions/day07.rs](src/solutions/day07.rs) | ⭐️⭐️ | | 8 | [src/solutions/day08.rs](src/solutions/day08.rs) | ⭐️⭐️ | - +| 9 | [src/solutions/day09.rs](src/solutions/day09.rs) | ⭐️⭐️ | diff --git a/src/solutions/day09.rs b/src/solutions/day09.rs index e7905a3..9dbd4e2 100644 --- a/src/solutions/day09.rs +++ b/src/solutions/day09.rs @@ -1,6 +1,5 @@ -use std::num::ParseIntError; - use crate::data::load; +use std::num::ParseIntError; use thiserror::Error; #[derive(Error, Debug, PartialEq, Eq)] @@ -48,6 +47,17 @@ pub fn puzzle_1(input: &str) -> Result { Ok(seqs.iter().map(|s| oasis_prediction(s)).sum()) } +pub fn puzzle_2(input: &str) -> Result { + let seqs = parse_input(input)? + .iter_mut() + .map(|s| { + s.reverse(); + s.clone() + }) + .collect::>(); + Ok(seqs.iter().map(|s| oasis_prediction(s)).sum()) +} + pub fn main(data_dir: &str) { println!("Day 9: Mirage Maintenance"); let data = load(data_dir, 9, None); @@ -61,10 +71,10 @@ pub fn main(data_dir: &str) { assert_eq!(answer_1, Ok(1666172641)); // Puzzle 2. - // let answer_2 = puzzle_2(&data); - // match answer_2 { - // Ok(x) => println!(" Puzzle 2: {}", x), - // Err(e) => panic!("No solution to puzzle 2: {}", e), - // } - // assert_eq!(answer_2, Ok(21003205388413)) + let answer_2 = puzzle_2(&data); + match answer_2 { + Ok(x) => println!(" Puzzle 2: {}", x), + Err(e) => panic!("No solution to puzzle 2: {}", e), + } + assert_eq!(answer_2, Ok(933)) } diff --git a/tests/test_day09.rs b/tests/test_day09.rs index bae4a58..03cb9f4 100644 --- a/tests/test_day09.rs +++ b/tests/test_day09.rs @@ -1,4 +1,4 @@ -use aoc_2023::solutions::day09::puzzle_1; +use aoc_2023::solutions::day09::{puzzle_1, puzzle_2}; const EXAMPLE_INPUT_1: &str = " 0 3 6 9 12 15 @@ -11,3 +11,9 @@ fn example_1_puzzle_1() { let _ = env_logger::try_init(); assert_eq!(puzzle_1(self::EXAMPLE_INPUT_1), Ok(114)); } + +#[test] +fn example_1_puzzle_2() { + let _ = env_logger::try_init(); + assert_eq!(puzzle_2(self::EXAMPLE_INPUT_1), Ok(2)); +}