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 content = read_file(name);
|
||||||
let lines = split_lines(&content);
|
let lines = split_lines(&content);
|
||||||
let input = split_inputs(lines, &r"\s+");
|
let input = split_inputs(lines, &r"\s+");
|
||||||
|
|
||||||
let sequences = convert_num(input);
|
let sequences = convert_num(input);
|
||||||
|
let structures: Vec<Vec<Vec<i32>>> = sequences
|
||||||
let sum_input_0: i32 = sequences[0]
|
.iter()
|
||||||
.clone()
|
.map(|s| extrapolate(s.to_vec()))
|
||||||
.into_iter()
|
.collect();
|
||||||
.sum();
|
|
||||||
println!("Erste Summe: {sum_input_0}");
|
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