diff --git a/src/bin/day06.rs b/src/bin/day06.rs index 83e2f7f..f9c99ac 100644 --- a/src/bin/day06.rs +++ b/src/bin/day06.rs @@ -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, } @@ -63,20 +69,14 @@ impl Solution 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 } }