day(06): introduce a helper function for filtering
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
cb0f7fa445
commit
19c94a7c46
1 changed files with 8 additions and 8 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue