-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
28 lines (27 loc) · 843 Bytes
/
main.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
pub fn main() {
let input = include_bytes!("../input.txt");
let newline = input.iter().position(|b| b == &b'\n').unwrap();
let time = atoi::atoi::<usize>(
&input[11..newline]
.iter()
.filter(|b| b != &&b' ')
.copied()
.collect::<Vec<_>>(),
)
.unwrap();
let dist = atoi::atoi::<usize>(
&input[newline + 12..]
.iter()
.filter(|b| b != &&b' ')
.copied()
.collect::<Vec<_>>(),
)
.unwrap();
// Modified quadratic formula: <https://en.wikipedia.org/wiki/Quadratic_formula>
let a = (time - f64::sqrt((time * time - 4 * dist) as f64) as usize) / 2;
let b = time - a;
println!(
"{}",
b - (b * (time - b) <= dist) as usize - a - (a * (time - a) <= dist) as usize + 1
);
}