1+ // Strategy: Identify the coordinates with infinite area by determining the
2+ // bounding box (inclusive) for the coordinates as a whole and then shrinking
3+ // it as follows:
4+ //
5+ // - For each side of the bounding box:
6+ // - Loop:
7+ // - Move the side one unit inwards.
8+ // - If there exists a point along the side whose closest coordinates
9+ // are all within the bounds, exit the loop.
10+ //
11+ // Afterwards, the coordinates outside the bounds are the ones with infinite
12+ // area, and the bounding box contains all points whose nearest coordinate is
13+ // one with finite area.
114use adventutil:: Input ;
215use adventutil:: counter:: Counter ;
316use adventutil:: gridgeom:: { Point , PointBounds } ;
@@ -14,6 +27,7 @@ fn solve(input: Input) -> u64 {
1427 let mut bounds = PointBounds :: for_points ( coords. iter ( ) . copied ( ) ) . unwrap ( ) ;
1528
1629 ' minx: loop {
30+ bounds. min_x += 1 ;
1731 let mut points_outside = Vec :: new ( ) ;
1832 let mut points_inside = Vec :: new ( ) ;
1933 for & p in & coords {
@@ -41,10 +55,10 @@ fn solve(input: Input) -> u64 {
4155 }
4256 }
4357 }
44- bounds. min_x += 1 ;
4558 }
4659
4760 ' maxx: loop {
61+ bounds. max_x -= 1 ;
4862 let mut points_outside = Vec :: new ( ) ;
4963 let mut points_inside = Vec :: new ( ) ;
5064 for & p in & coords {
@@ -72,10 +86,10 @@ fn solve(input: Input) -> u64 {
7286 }
7387 }
7488 }
75- bounds. max_x -= 1 ;
7689 }
7790
7891 ' miny: loop {
92+ bounds. min_y += 1 ;
7993 let mut points_outside = Vec :: new ( ) ;
8094 let mut points_inside = Vec :: new ( ) ;
8195 for & p in & coords {
@@ -103,10 +117,10 @@ fn solve(input: Input) -> u64 {
103117 }
104118 }
105119 }
106- bounds. min_y += 1 ;
107120 }
108121
109122 ' maxy: loop {
123+ bounds. max_y -= 1 ;
110124 let mut points_outside = Vec :: new ( ) ;
111125 let mut points_inside = Vec :: new ( ) ;
112126 for & p in & coords {
@@ -134,7 +148,6 @@ fn solve(input: Input) -> u64 {
134148 }
135149 }
136150 }
137- bounds. max_y -= 1 ;
138151 }
139152
140153 let mut counter = Counter :: new ( ) ;
0 commit comments