-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay17.rs
58 lines (46 loc) · 1.39 KB
/
Day17.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::env;
use std::io;
fn main() {
let desc = io::stdin().lines().next().unwrap().unwrap();
let parts: Vec<i32> = desc
.split(|c| matches!(c, '=' | '.' | ',' | ' '))
.filter_map(|s| s.parse().ok())
.collect();
let guess = 300;
let mut total_max_y = 0;
let mut hit_count = 0;
if let [xmin, xmax, ymin, ymax] = parts[..] {
for xv in 0..=xmax + 1 {
for yv in ymin - 1..=guess {
let mut xv = xv;
let mut yv = yv;
let mut x = 0;
let mut y = 0;
let mut max_y = 0;
let mut hit = false;
loop {
x += xv;
y += yv;
xv -= xv.signum();
yv -= 1;
max_y = max_y.max(y);
if xmin <= x && x <= xmax && ymin <= y && y <= ymax {
hit = true;
}
if y < ymin || x > xmax || (xv == 0 && x < xmin) {
break;
}
}
if hit {
total_max_y = total_max_y.max(max_y);
hit_count += 1;
}
}
}
}
if env::args().nth(1).unwrap() == "1" {
println!("{}", total_max_y);
} else {
println!("{}", hit_count);
}
}