From 793241a355b00a70f8a45514c5407b920d1d452d Mon Sep 17 00:00:00 2001 From: Mathis Date: Wed, 20 Dec 2023 23:18:31 +0100 Subject: [PATCH] 2023-09: Solve part 2 --- .../day_09_mirage_maintenance/common.rs | 11 +++++++ .../day_09_mirage_maintenance/mod.rs | 11 +++---- .../day_09_mirage_maintenance/part_one.rs | 14 ++------- .../day_09_mirage_maintenance/part_two.rs | 29 +++++++++++++++++++ 4 files changed, 46 insertions(+), 19 deletions(-) create mode 100644 src/year_2023/day_09_mirage_maintenance/common.rs create mode 100644 src/year_2023/day_09_mirage_maintenance/part_two.rs diff --git a/src/year_2023/day_09_mirage_maintenance/common.rs b/src/year_2023/day_09_mirage_maintenance/common.rs new file mode 100644 index 0000000..8102f75 --- /dev/null +++ b/src/year_2023/day_09_mirage_maintenance/common.rs @@ -0,0 +1,11 @@ +pub fn parse_line(s: &str) -> Vec { + s.split_whitespace().map(|x| x.parse().unwrap()).collect() +} + +pub fn get_differences(nums: Vec) -> Vec { + nums.windows(2).map(|w| w[1] - w[0]).collect() +} + +pub fn all_zeros(nums: Vec) -> bool { + nums.iter().all(|&x| x == 0) +} \ No newline at end of file diff --git a/src/year_2023/day_09_mirage_maintenance/mod.rs b/src/year_2023/day_09_mirage_maintenance/mod.rs index 7bcfe68..49f0c19 100644 --- a/src/year_2023/day_09_mirage_maintenance/mod.rs +++ b/src/year_2023/day_09_mirage_maintenance/mod.rs @@ -1,5 +1,6 @@ pub mod part_one; -// pub mod part_two; +pub mod part_two; +pub mod common; use crate::utils::solution::Solution; use crate::utils::input_reader; @@ -8,16 +9,12 @@ use crate::utils::input_reader; pub struct Day9Solution; impl Solution for Day9Solution { + fn part_one(&self) -> String { - // Implementation for part one of day 1 - // ... input_reader::read_input_file("input/2023/day_09/input.txt", part_one::part_one) } fn part_two(&self) -> String { - // Implementation for part two of day 1 - // ... - String::from("Not implemented") - // part_two::part_two() + input_reader::read_input_file("input/2023/day_09/input.txt", part_two::part_two) } } diff --git a/src/year_2023/day_09_mirage_maintenance/part_one.rs b/src/year_2023/day_09_mirage_maintenance/part_one.rs index 18add56..b6d37fc 100644 --- a/src/year_2023/day_09_mirage_maintenance/part_one.rs +++ b/src/year_2023/day_09_mirage_maintenance/part_one.rs @@ -1,3 +1,5 @@ +use super::common::{parse_line, get_differences, all_zeros}; + pub fn part_one(input_lines: Vec) -> String { let mut result: i32 = 0; @@ -19,16 +21,4 @@ pub fn part_one(input_lines: Vec) -> String { } result.to_string() -} - -fn parse_line(s: &str) -> Vec { - s.split_whitespace().map(|x| x.parse().unwrap()).collect() -} - -fn get_differences(nums: Vec) -> Vec { - nums.windows(2).map(|w| w[1] - w[0]).collect() -} - -fn all_zeros(nums: Vec) -> bool { - nums.iter().all(|&x| x == 0) } \ No newline at end of file diff --git a/src/year_2023/day_09_mirage_maintenance/part_two.rs b/src/year_2023/day_09_mirage_maintenance/part_two.rs new file mode 100644 index 0000000..ab1f425 --- /dev/null +++ b/src/year_2023/day_09_mirage_maintenance/part_two.rs @@ -0,0 +1,29 @@ +use super::common::{parse_line, get_differences, all_zeros}; + +pub fn part_two(input_lines: Vec) -> String { + let mut result: i32 = 0; + + for line in input_lines { + let nums = parse_line(&line); + + let mut first: Vec = Vec::new(); + first.push(nums.first().unwrap().clone()); + + let mut diffs = get_differences(nums.clone()); + first.push(diffs.first().unwrap().clone()); + + while !all_zeros(diffs.clone()) { + diffs = get_differences(diffs.clone()); + first.push(diffs.first().unwrap().clone()); + } + + first.reverse(); + + let mut res = 0; + res = first.iter().fold(res, |acc, &x| x - acc); + + result += res; + } + + result.to_string() +} \ No newline at end of file