Compare commits

..

11 Commits

2 changed files with 101 additions and 16 deletions

View File

@ -1,28 +1,95 @@
use crate::common::{convert_num, read_file, split_inputs, split_lines};
pub fn main() {
let name = "day09";
println!("Executing module '{name}' entrypoint . . . ");
#[allow(dead_code)]
pub fn part_two() {
predict(false);
}
let content = read_file(name);
let lines = split_lines(&content);
let input = split_inputs(lines, &r"\s+");
#[allow(dead_code)]
pub fn part_one() {
predict(true);
}
let sequences = convert_num(input);
let structures: Vec<Vec<Vec<i32>>> = sequences
fn predict(forward: bool) {
let sequences = setup();
let predictions: Vec<i32> = sequences
.iter()
.map(|s| extrapolate(s.to_vec()))
.map(|s| {
let current = s.to_vec();
let mut next = extra_diff(current);
expand(&mut next, forward);
let history = next
.first()
.unwrap();
if forward {
history
.last()
.unwrap()
.clone()
} else {
history
.first()
.unwrap()
.clone()
}
})
.collect();
for seq in structures[0].clone() {
for n in seq {
print!("{n} ");
let sum: i32 = predictions
.iter()
.sum();
println!("The sum of all prediction values is: '{sum}'.");
}
fn expand(strct: &mut Vec<Vec<i32>>, forward: bool) {
let last = strct
.last_mut()
.unwrap();
last.push(*last
.last()
.unwrap()
);
for i in (0..strct.len() - 1).rev() {
let last = strct
.get(i + 1)
.unwrap()
.clone();
let current = strct
.get_mut(i)
.unwrap();
if forward {
current.push(
current
.last()
.unwrap()
.clone() +
last
.last()
.unwrap()
.clone()
);
} else {
current.insert(
0,
current
.first()
.unwrap()
.clone() -
last
.first()
.unwrap()
.clone()
);
}
println!("");
}
}
fn extrapolate(seq: Vec<i32>) -> Vec<Vec<i32>> {
fn extra_diff(seq: Vec<i32>) -> Vec<Vec<i32>> {
let mut structure = vec![seq];
let mut index = 0;
@ -48,3 +115,13 @@ fn seq_diff(seq: Vec<i32>) -> Vec<i32> {
diff
}
fn setup() -> Vec<Vec<i32>> {
let name = "day09";
println!("Executing module '{name}' entrypoint . . . ");
let content = read_file(name);
let lines = split_lines(&content);
let input = split_inputs(lines, &r"\s+");
convert_num(input)
}

View File

@ -1,7 +1,15 @@
use std::time::Instant;
mod common;
mod day09;
fn main() {
println!("Executing main entrypoint . . . ");
day09::main();
let now = Instant::now();
// day09::part_one();
day09::part_two();
let elapsed = now.elapsed();
println!("Execution in {:?}.", elapsed);
}