Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/advent-of-code-2021
Path: blob/main/src/day17.rs
97 views
1
pub fn run() {
2
// Doesn't really make sense to parse this input.
3
let area_x = 48..=70;
4
let area_y = -189..=-148;
5
6
let probe_hits_area = |v0: (i32, i32)| {
7
let (mut x, mut y) = (0, 0);
8
let (mut vx, mut vy) = v0;
9
while x < *area_x.end() && y > *area_y.start() {
10
x += vx;
11
y += vy;
12
vx -= vx.min(1);
13
vy -= 1;
14
if area_x.contains(&x) && area_y.contains(&y) {
15
return true;
16
}
17
}
18
false
19
};
20
21
let mut successful_velocities = vec![];
22
// Values sort of eyeballed after trying out larger limits.
23
for vx0 in 0..100 {
24
for vy0 in -200..200 {
25
if probe_hits_area((vx0, vy0)) {
26
successful_velocities.push((vx0, vy0));
27
}
28
}
29
}
30
31
let max_vy = successful_velocities
32
.iter()
33
.map(|(_, vy)| vy)
34
.max()
35
.unwrap();
36
let max_height = (max_vy + 1) * max_vy / 2;
37
38
println!("{}", max_height);
39
println!("{}", successful_velocities.len());
40
}
41
42