-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
56 lines (50 loc) · 1.24 KB
/
mod.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
fn ways_to_win(time: usize, record: usize) -> usize {
let mut min = 0;
let mut max = 0;
for i in 0..time {
let distance = i * (time - i);
if distance > record {
min = i;
break;
}
}
for i in (0..time).rev() {
let distance = i * (time - i);
if distance > record {
max = i;
break;
}
}
max - min + 1
}
pub fn solve(input: String) {
let mut res_one = 1;
let data_one: Vec<Vec<usize>> = input
.lines()
.map(|l| {
l[10..]
.split_ascii_whitespace()
.map(|w| w.parse().unwrap())
.collect()
})
.collect();
assert_eq!(data_one[0].len(), data_one[1].len());
for i in 0..data_one[0].len() {
res_one *= ways_to_win(data_one[0][i], data_one[1][i]);
}
let data_two: Vec<usize> = input
.lines()
.map(|l| {
l[10..]
.chars()
.filter(|c| !c.is_whitespace())
.collect::<String>()
.parse()
.unwrap()
})
.collect();
aoc::print_solution(&[
res_one,
ways_to_win(data_two[0], data_two[1]),
])
}