This commit is contained in:
Shadowfacts 2021-12-17 22:34:59 -05:00
parent c7b9a878ec
commit 260f0cd6c3
2 changed files with 42 additions and 1 deletions

40
src/day17.rs Normal file
View File

@ -0,0 +1,40 @@
use std::cmp::max;
use std::ops::RangeInclusive;
pub fn day17() {
solve(&(20..=30), &(-10..=-5));
solve(&(287..=309), &(-76..=-48));
}
fn solve(target_x: &RangeInclusive<isize>, target_y: &RangeInclusive<isize>) {
let maxes = (0..=*target_x.end()).flat_map(|vx| {
(*target_y.start()..=100).filter_map(move |vy| count_steps(vx, vy, target_x, target_y))
});
dbg!(maxes.clone().max());
dbg!(maxes.count());
}
fn count_steps(
mut vx: isize,
mut vy: isize,
target_x: &RangeInclusive<isize>,
target_y: &RangeInclusive<isize>,
) -> Option<isize> {
let mut x = 0;
let mut y = 0;
let mut max_y = 0;
loop {
x += vx;
y += vy;
vx -= vx.signum();
vy -= 1;
max_y = max(max_y, y);
if target_x.contains(&x) && target_y.contains(&y) {
return Some(max_y);
} else if !target_x.contains(&x) && vx == 0 {
return None;
} else if !target_y.contains(&y) && y < *target_y.start() {
return None;
}
}
}

View File

@ -15,7 +15,8 @@ mod day13;
mod day14; mod day14;
mod day15; mod day15;
mod day16; mod day16;
mod day17;
fn main() { fn main() {
day16::day16(); day17::day17();
} }