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,
|
distance: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Race {
|
||||||
|
fn is_better(&self, t: i64) -> bool {
|
||||||
|
t * (self.time - t) > self.distance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct Day06 {
|
struct Day06 {
|
||||||
races: Vec<Race>,
|
races: Vec<Race>,
|
||||||
}
|
}
|
||||||
|
@ -63,20 +69,14 @@ impl Solution<Output1, Output2> for Day06 {
|
||||||
fn part_1(&mut self) -> Output1 {
|
fn part_1(&mut self) -> Output1 {
|
||||||
self.races
|
self.races
|
||||||
.iter()
|
.iter()
|
||||||
.map(|r| {
|
.map(|r| (1..r.time).filter(|t| r.is_better(*t)).count() as i32)
|
||||||
(1..r.time)
|
|
||||||
.filter(|t| (r.time - t) * t > r.distance)
|
|
||||||
.count() as i32
|
|
||||||
})
|
|
||||||
.product()
|
.product()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(&mut self) -> Output2 {
|
fn part_2(&mut self) -> Output2 {
|
||||||
let merged = self.merge_races();
|
let merged = self.merge_races();
|
||||||
|
|
||||||
(1..merged.time)
|
(1..merged.time).filter(|t| merged.is_better(*t)).count() as i32
|
||||||
.filter(|t| (merged.time - t) * t > merged.distance)
|
|
||||||
.count() as i32
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue