day 9 part 1 & 2

This commit is contained in:
OMGeeky
2024-01-30 10:42:43 +00:00
parent db2e82f64c
commit 4e45969df9
4 changed files with 306 additions and 1 deletions

98
src/day09.rs Normal file
View File

@@ -0,0 +1,98 @@
use crate::*;
pub struct Day09;
impl Day for Day09 {
const DAY_NUM: u8 = 09;
type Input = String;
type Output = i32;
fn get_test_data() -> Self::Input {
"0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45".to_string()
}
fn get_test_result() -> Self::Output {
114
}
fn run(data: Self::Input) -> Self::Output {
data.lines()
.map(|x|{
x.trim().split(' ').map(|x|{
// println!("{}",x);
x.parse::<i32>().unwrap()
})
}).map(|x|{
get_sub_rec(&x.collect())
})
.map(|x:i32|{
x
}).sum()
}
}
fn get_sub_rec<'a>(x: &Vec<i32>) -> i32 {
println!("{:?}", x);
let x2 = get_sub(x.iter()).collect::<Vec<_>>();
println!("=> {:?}", &x2);
if x2.len() < 2 {
dbg!(x2);
todo!("What to do if we are all the way down?")
}
let last = *x.last().unwrap();
if x2[0] == 0 && x2[1] == 0{
return last;
}
let sub = get_sub_rec(&x2);
dbg!(&sub, &last, sub + last);
last + sub
}
fn get_sub<'a>(x: impl Iterator<Item = &'a i32> + 'a)-> Box<dyn Iterator<Item = i32> + 'a>{
Box::new(x.map_windows(|[a,b]|{
*b-(*a)
}))
}
impl DayPart2 for Day09{
fn run_part2(data: Self::Input) -> Self::Output { data.lines()
.map(|x|{
x.trim().split(' ').map(|x|{
// println!("{}",x);
x.parse::<i32>().unwrap()
})
}).map(|x|{
get_sub_rec_part2(&x.collect())
})
.map(|x:i32|{
x
}).sum()
}
fn get_test_result_part2() -> Self::Output {
2
}
fn get_test_data_part2() -> Self::Input {
Self::get_test_data()
}
}
fn get_sub_rec_part2<'a>(x: &Vec<i32>) -> i32 {
println!("{:?}", x);
let x2 = get_sub(x.iter()).collect::<Vec<_>>();
println!("=> {:?}", &x2);
if x2.len() < 2 {
dbg!(x2);
todo!("What to do if we are all the way down?")
}
let last = *x.first().unwrap();
if x2[0] == 0 && x2[1] == 0{
return last;
}
let sub = get_sub_rec_part2(&x2);
dbg!(&sub, &last, sub - last);
last - sub
}

View File

@@ -1,4 +1,5 @@
#![feature(test)]
#![feature(iter_map_windows)]
pub fn read_input<T>(day: u8) -> T
where
@@ -25,6 +26,9 @@ pub use day06::*;
mod day07;
pub use day07::*;
mod day09;
pub use day09::*;
mod day19;
pub use day19::*;

View File

@@ -9,8 +9,11 @@ fn main() {
// Day05::run_all();
//Day06::run_all();
//Day07::run_all();
// Day09::run_all();
Day09::part2();
//Day19::run_all();
Day19::part2();
// Day19::part2();
// Day19::part2();
// utils::run!(01);
// utils::run!(02);