From 5cf79c7fe2c14fc830f567d61abbe4c75b5f9b8c Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Wed, 24 Jan 2024 18:38:59 +0100 Subject: [PATCH] differentiate sequence and extrapolate --- src/day09/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/day09/mod.rs b/src/day09/mod.rs index ef4c54c..69e51e2 100644 --- a/src/day09/mod.rs +++ b/src/day09/mod.rs @@ -7,11 +7,44 @@ pub fn main() { let content = read_file(name); let lines = split_lines(&content); let input = split_inputs(lines, &r"\s+"); + let sequences = convert_num(input); - - let sum_input_0: i32 = sequences[0] - .clone() - .into_iter() - .sum(); - println!("Erste Summe: {sum_input_0}"); + let structures: Vec>> = sequences + .iter() + .map(|s| extrapolate(s.to_vec())) + .collect(); + + for seq in structures[0].clone() { + for n in seq { + print!("{n} "); + } + println!(""); + } +} + +fn extrapolate(seq: Vec) -> Vec> { + let mut structure = vec![seq]; + let mut index = 0; + + loop { + let current = &structure[index]; + let diff = seq_diff(current.to_vec()); + + if diff.iter().all(|&n| n == 0) { + break structure; + } + + structure.push(diff); + index += 1; + } +} + +fn seq_diff(seq: Vec) -> Vec { + let mut diff = Vec::new(); + + for (i, n) in seq.iter().skip(1).enumerate() { + diff.push(n - seq[i]); + } + + diff }