Day 09 Part 2 Complete

This commit is contained in:
2023-12-09 14:07:04 +00:00
parent 5627684494
commit a4c8d73439
5 changed files with 286 additions and 8 deletions
+79 -7
View File
@@ -5,16 +5,18 @@ pub struct Day09 {}
impl Solution for Day09 {
fn part1(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
let series_collection = self.get_series(input)?;
Ok(Box::new(self.sum_extrapolated(&series_collection, true)?))
}
fn part2(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display + Sync>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
let series_collection = self.get_series(input)?;
Ok(Box::new(self.sum_extrapolated(&series_collection, false)?))
}
fn get_day(&self) -> u8 {
@@ -22,7 +24,77 @@ impl Solution for Day09 {
}
}
impl Day09 {}
impl Day09 {
/// Takes a vec of strings and returns a Vec of series of numbers
fn get_series(&self, input: &Vec<String>) -> Result<Vec<Vec<i32>>, Box<dyn std::error::Error>> {
let mut series = vec![];
for s in input.iter() {
series.push(
s.split_whitespace()
.map(|n| n.parse::<i32>().unwrap())
.collect(),
);
}
Ok(series)
}
/// Returns the sum ov extrapolated values either to the left or the right of the initial series
fn sum_extrapolated(
&self,
series_collection: &Vec<Vec<i32>>,
forwards: bool,
) -> Result<i32, Box<dyn std::error::Error>> {
let mut ans = 0i32;
for series in series_collection {
let mut differences = vec![series.clone()];
// Keep finding differences until we have a consistent difference of 0
while differences
.last()
.unwrap()
.iter()
.map(|v| v.abs())
.sum::<i32>()
!= 0i32
{
differences.push(
differences
.last()
.unwrap()
.windows(2)
.map(|w| w[1] - w[0])
.collect::<Vec<i32>>(),
);
}
if forwards {
// Sum all of the last values in each row to get next num
ans += differences
.iter()
.map(|s| *s.last().unwrap())
.collect::<Vec<i32>>()
.iter()
.sum::<i32>();
} else {
// Scale backwards down the heap to find the previous number in each sequence
let mut next = 0i32;
differences
.iter()
.rev()
.map(|s| *s.first().unwrap())
.collect::<Vec<i32>>()
.iter()
.for_each(|v| {
next = v - next;
});
ans += next;
}
}
Ok(ans)
}
}
/// Test from puzzle input
#[cfg(test)]
@@ -44,7 +116,7 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "114");
}
#[test]
@@ -61,6 +133,6 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "2");
}
}