Skip to content

Commit

Permalink
Day 9 puzzle 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jhrcook committed Dec 9, 2023
1 parent 2952f48 commit 00f490e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | ⭐️⭐️ | -->
| 9 | [src/solutions/day09.rs](src/solutions/day09.rs) | ⭐️⭐️ |
<!-- | 10 | [src/solutions/day10.rs](src/solutions/day10.rs) | ⭐️⭐️ | -->
<!-- | 11 | [src/solutions/day11.rs](src/solutions/day11.rs) | ⭐️⭐️ | -->
<!-- | 12 | [src/solutions/day12.rs](src/solutions/day12.rs) | ⭐️⭐️ | -->
Expand Down
26 changes: 18 additions & 8 deletions src/solutions/day09.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::num::ParseIntError;

use crate::data::load;
use std::num::ParseIntError;
use thiserror::Error;

#[derive(Error, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -48,6 +47,17 @@ pub fn puzzle_1(input: &str) -> Result<i32, PuzzleErr> {
Ok(seqs.iter().map(|s| oasis_prediction(s)).sum())
}

pub fn puzzle_2(input: &str) -> Result<i32, PuzzleErr> {
let seqs = parse_input(input)?
.iter_mut()
.map(|s| {
s.reverse();
s.clone()
})
.collect::<Vec<_>>();
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);
Expand All @@ -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))
}
8 changes: 7 additions & 1 deletion tests/test_day09.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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));
}

0 comments on commit 00f490e

Please sign in to comment.