1
0
Fork 0

day(06): introduce a helper function for filtering

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-12-06 17:09:28 +01:00
parent cb0f7fa445
commit 19c94a7c46
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -9,6 +9,12 @@ struct Race {
distance: i64,
}
impl Race {
fn is_better(&self, t: i64) -> bool {
t * (self.time - t) > self.distance
}
}
struct Day06 {
races: Vec<Race>,
}
@ -63,20 +69,14 @@ impl Solution<Output1, Output2> for Day06 {
fn part_1(&mut self) -> Output1 {
self.races
.iter()
.map(|r| {
(1..r.time)
.filter(|t| (r.time - t) * t > r.distance)
.count() as i32
})
.map(|r| (1..r.time).filter(|t| r.is_better(*t)).count() as i32)
.product()
}
fn part_2(&mut self) -> Output2 {
let merged = self.merge_races();
(1..merged.time)
.filter(|t| (merged.time - t) * t > merged.distance)
.count() as i32
(1..merged.time).filter(|t| merged.is_better(*t)).count() as i32
}
}