mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-11-15 02:03: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 sum_input_0: i32 = sequences[0]
|
let sequences = convert_num(input);
|
||||||
.clone()
|
let structures: Vec<Vec<Vec<i32>>> = sequences
|
||||||
.into_iter()
|
.iter()
|
||||||
.sum();
|
.map(|s| extrapolate(s.to_vec()))
|
||||||
println!("Erste Summe: {sum_input_0}");
|
.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