Skip to content

Commit e1d24c9

Browse files
committed
refactor(09/2025): add some simplifications
1 parent b546dcb commit e1d24c9

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/solutions/year2025/day09.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ impl Solution for Day09 {
2020

2121
fn part_two(&self, input: &str) -> String {
2222
let points = self.parse(input);
23-
let polygon = points.clone().collect::<Polygon>();
23+
let region: FilledRegion = points.clone().collect::<Polygon>().into();
2424

2525
points
2626
.tuple_combinations()
2727
.filter_map(|(a, b)| {
2828
let rectangle = Polygon::rectangle(a, b);
29-
if polygon.is_inside(&rectangle) {
30-
return Some(FilledRegion::from(rectangle).area()); // optimize...
29+
if region.is_inside(&rectangle) {
30+
return Some(SurfaceRange::from((a, b)).area());
3131
}
3232

3333
None

src/utils/line.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::utils::orientation::Orientation;
22
use crate::utils::point::Point;
3+
use crate::utils::range::Range;
34

45
#[derive(Debug, Copy, Clone, PartialEq)]
56
pub struct Line {
@@ -47,6 +48,7 @@ impl Line {
4748
Some(Point::new(x as isize, y as isize))
4849
}
4950

51+
// todo extract line that can be only horizontal and vertical
5052
fn orientation(&self) -> Option<Orientation> {
5153
let a = self.start;
5254
let b = self.end;
@@ -70,23 +72,14 @@ impl Line {
7072
let end = self.end;
7173

7274
return match orientation {
73-
Orientation::Horizontal => {
74-
let mut points = Vec::new();
75-
// todo range from unordered ?
76-
for x in start.x.min(end.x)..=start.x.max(end.x) {
77-
points.push(Point::new(x, start.y));
78-
}
79-
80-
points
81-
}
82-
Orientation::Vertical => {
83-
let mut points = Vec::new();
84-
for y in start.y.min(end.y)..=start.y.max(end.y) {
85-
points.push(Point::new(start.x, y));
86-
}
87-
88-
points
89-
}
75+
Orientation::Horizontal => Range::from_unordered(start.x, end.x)
76+
.iter()
77+
.map(|x| Point::new(x, start.y))
78+
.collect(),
79+
Orientation::Vertical => Range::from_unordered(start.y, end.y)
80+
.iter()
81+
.map(|y| Point::new(start.x, y))
82+
.collect(),
9083
};
9184
}
9285

0 commit comments

Comments
 (0)