Function aoc::y23d09::y23d09

source ·
pub fn y23d09(input: &str, part: u32) -> i32
Expand description

The solution for the day nine challenge.

As usual we take the input as a string and an integer to denote the part. In part 1 we need to find the next number in the sequence and in part 2 we need to find the first number in the sequence. For each sequence (line) we split the input and parse the numbers. Then, we find the diffs between each of the numbers. If all of the diffs are zero then we’re done finding the diffs. Otherwise we add the new list of diffs to a vector of diffs and then run the same process on that list continuing until we have all zeros. Then we calculate the previous and next numbers in the sequence. We reverse the list of diffs that we have and add the previous value (zero to start) to the next item that we pop from the beginning or the end of the diff that we’re processing. When we’re done we’ve found the next (or previous) number in the sequence and can add it to the vector tracking all of the sequences. Finally, based on the part we can return the sum of the next numbers or the previous numbers.

§Example

// probably read this from the input file...
let input = "9 12 17 23 28 40";
assert_eq!(y23d09(input, 1), 88);
assert_eq!(y23d09(input, 2), -3);