mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-11-15 02:03:49 +00:00
implement p2 with direction toggle
This commit is contained in:
parent
f5ea37e0d8
commit
03a9838f4b
@ -2,23 +2,37 @@ use crate::common::{convert_num, read_file, split_inputs, split_lines};
|
|||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn part_two() {
|
pub fn part_two() {
|
||||||
unimplemented!();
|
predict(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn part_one() {
|
pub fn part_one() {
|
||||||
|
predict(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn predict(forward: bool) {
|
||||||
let sequences = setup();
|
let sequences = setup();
|
||||||
let predictions: Vec<i32> = sequences
|
let predictions: Vec<i32> = sequences
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| {
|
.map(|s| {
|
||||||
let current = s.to_vec();
|
let current = s.to_vec();
|
||||||
let mut next = extra_diff(current);
|
let mut next = extra_diff(current);
|
||||||
expand(&mut next);
|
expand(&mut next, forward);
|
||||||
next.first()
|
let history = next
|
||||||
.unwrap()
|
.first()
|
||||||
.last()
|
.unwrap();
|
||||||
.unwrap()
|
|
||||||
.clone()
|
if forward {
|
||||||
|
history
|
||||||
|
.last()
|
||||||
|
.unwrap()
|
||||||
|
.clone()
|
||||||
|
} else {
|
||||||
|
history
|
||||||
|
.first()
|
||||||
|
.unwrap()
|
||||||
|
.clone()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
@ -29,7 +43,7 @@ pub fn part_one() {
|
|||||||
println!("The sum of all prediction values is: '{sum}'.");
|
println!("The sum of all prediction values is: '{sum}'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand(strct: &mut Vec<Vec<i32>>) {
|
fn expand(strct: &mut Vec<Vec<i32>>, forward: bool) {
|
||||||
let last = strct
|
let last = strct
|
||||||
.last_mut()
|
.last_mut()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -48,17 +62,30 @@ fn expand(strct: &mut Vec<Vec<i32>>) {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
|
||||||
let next =
|
if forward {
|
||||||
current
|
current.push(
|
||||||
.last()
|
current
|
||||||
.unwrap()
|
.last()
|
||||||
.clone() +
|
.unwrap()
|
||||||
last
|
.clone() +
|
||||||
.last()
|
last
|
||||||
.unwrap()
|
.last()
|
||||||
.clone();
|
.unwrap()
|
||||||
|
.clone()
|
||||||
current.push(next);
|
);
|
||||||
|
} else {
|
||||||
|
current.insert(
|
||||||
|
0,
|
||||||
|
current
|
||||||
|
.first()
|
||||||
|
.unwrap()
|
||||||
|
.clone() -
|
||||||
|
last
|
||||||
|
.first()
|
||||||
|
.unwrap()
|
||||||
|
.clone()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user