-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
80 lines (71 loc) · 1.58 KB
/
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
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use rayon::{join, prelude::*};
struct Run {
duration: u16,
record: u16,
}
impl Run {
fn hold(&self, hold: u16) -> bool {
let time_left = self.duration - hold;
let velocity = hold;
let distance = velocity * time_left;
distance > self.record
}
}
const INPUT: &[Run; 4] = &[
Run {
duration: 42,
record: 308,
},
Run {
duration: 89,
record: 1170,
},
Run {
duration: 91,
record: 1291,
},
Run {
duration: 89,
record: 1467,
},
];
fn a() {
let product: usize = INPUT
.par_iter()
.map(|run| {
(0..(run.duration))
.flat_map(|hold| run.hold(hold).then_some(hold))
.count()
})
.product();
println!("{product}");
}
fn b() {
let duration: u64 = 42899189;
let record: u64 = 308117012911467;
if let (Some(first_win), Some(last_win)) = join(
|| {
(0..=duration).find(|hold| {
let time_left = duration - hold;
let velocity = hold;
let distance = velocity * time_left;
distance > record
})
},
|| {
(0..=duration).rev().find(|hold| {
let time_left = duration - hold;
let velocity = hold;
let distance = velocity * time_left;
distance > record
})
},
) {
let wins = last_win - first_win + 1; // off by one
println!("{wins}");
}
}
fn main() {
a();
b();
}