mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-11-14 17:53:49 +00:00
differentiate sequence and extrapolate
This commit is contained in:
parent
30f9c1bf88
commit
5cf79c7fe2
@ -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<Vec<Vec<i32>>> = 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<i32>) -> Vec<Vec<i32>> {
|
||||
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<i32>) -> Vec<i32> {
|
||||
let mut diff = Vec::new();
|
||||
|
||||
for (i, n) in seq.iter().skip(1).enumerate() {
|
||||
diff.push(n - seq[i]);
|
||||
}
|
||||
|
||||
diff
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user