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}; use crate::common::{convert_num, read_file, split_inputs, split_lines};
pub fn main() { #[allow(dead_code)]
let name = "day09"; pub fn part_two() {
println!("Executing module '{name}' entrypoint . . . "); predict(false);
}
let content = read_file(name);
let lines = split_lines(&content);
let input = split_inputs(lines, &r"\s+");
let sequences = convert_num(input); #[allow(dead_code)]
let structures: Vec<Vec<Vec<i32>>> = sequences pub fn part_one() {
predict(true);
}
fn predict(forward: bool) {
let sequences = setup();
let predictions: Vec<i32> = sequences
.iter() .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(); .collect();
let sum: i32 = predictions
.iter()
.sum();
for seq in structures[0].clone() { println!("The sum of all prediction values is: '{sum}'.");
for n in seq { }
print!("{n} ");
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 structure = vec![seq];
let mut index = 0; let mut index = 0;
@ -48,3 +115,13 @@ fn seq_diff(seq: Vec<i32>) -> Vec<i32> {
diff 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 common;
mod day09; mod day09;
fn main() { fn main() {
println!("Executing main entrypoint . . . "); println!("Executing main entrypoint . . . ");
day09::main(); let now = Instant::now();
// day09::part_one();
day09::part_two();
let elapsed = now.elapsed();
println!("Execution in {:?}.", elapsed);
} }