mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-27 04:21:45 +00:00
Compare commits
11 Commits
633e1a8566
...
b38cb21b02
Author | SHA1 | Date | |
---|---|---|---|
b38cb21b02 | |||
03a9838f4b | |||
f5ea37e0d8 | |||
87ef3ea41e | |||
6c8ee04e95 | |||
ea0ffa5229 | |||
97c5310ead | |||
f86be23eca | |||
30695d92dd | |||
411784f438 | |||
630170e636 |
105
src/day09/mod.rs
105
src/day09/mod.rs
@ -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)
|
||||
}
|
||||
|
10
src/main.rs
10
src/main.rs
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user