|
@@ -120,7 +120,7 @@ fn fill_grid(
|
120
|
120
|
) -> Result<&mut Vec<GridPoint>, Box<Error>> {
|
121
|
121
|
for coord in coords {
|
122
|
122
|
let start_index = (coord.x * (boundary_coord.y + 1)) + coord.y;
|
123
|
|
- fill_grid_with_coordinate(grid, start_index, coord, boundary_coord)?;
|
|
123
|
+ fill_grid_with_coordinate(grid, start_index, coord, boundary_coord, 0)?;
|
124
|
124
|
}
|
125
|
125
|
Ok(grid)
|
126
|
126
|
}
|
|
@@ -130,22 +130,39 @@ fn fill_grid_with_coordinate(
|
130
|
130
|
index: u32,
|
131
|
131
|
coord: Coordinate,
|
132
|
132
|
boundary_coord: Coordinate,
|
|
133
|
+ iterations: u32,
|
133
|
134
|
) -> Result<&mut Vec<GridPoint>, Box<Error>> {
|
|
135
|
+ if iterations == 10 { return Ok(grid); }
|
134
|
136
|
println!("index: {}", index);
|
135
|
|
- let point = &mut grid.get(index as usize).unwrap();
|
|
137
|
+ println!("grid: {:?}", grid);
|
|
138
|
+ let point = &mut grid.get_mut(index as usize).unwrap();
|
136
|
139
|
match point {
|
137
|
140
|
GridPoint::Unfilled { x, y } => {
|
138
|
|
- mem::replace(
|
139
|
|
- point,
|
140
|
|
- &GridPoint::Filled {
|
|
141
|
+ grid[index as usize] = GridPoint::Filled {
|
|
142
|
+ x: *x,
|
|
143
|
+ y: *y,
|
|
144
|
+ closest_coord: coord,
|
|
145
|
+ closest_dist: manhattan_dist(coord.x, coord.y, *x, *y),
|
|
146
|
+ };
|
|
147
|
+ },
|
|
148
|
+ GridPoint::Tied { x, y, closest_dist } |
|
|
149
|
+ GridPoint::Filled { x, y, closest_coord: _, closest_dist } => {
|
|
150
|
+ let dist = manhattan_dist(coord.x, coord.y, *x, *y);
|
|
151
|
+ if dist < *closest_dist {
|
|
152
|
+ grid[index as usize] = GridPoint::Filled {
|
141
|
153
|
x: *x,
|
142
|
154
|
y: *y,
|
143
|
155
|
closest_coord: coord,
|
144
|
|
- closest_dist: manhattan_dist(coord.x, coord.y, *x, *y),
|
145
|
|
- });
|
|
156
|
+ closest_dist: dist,
|
|
157
|
+ };
|
|
158
|
+ } else if dist == *closest_dist {
|
|
159
|
+ grid[index as usize] = GridPoint::Tied {
|
|
160
|
+ x: *x,
|
|
161
|
+ y: *y,
|
|
162
|
+ closest_dist: dist,
|
|
163
|
+ };
|
|
164
|
+ }
|
146
|
165
|
},
|
147
|
|
- GridPoint::Tied { x: _, y: _, closest_dist: _ } => {},
|
148
|
|
- GridPoint::Filled { x: _, y: _, closest_coord: _, closest_dist: _ } => {},
|
149
|
166
|
}
|
150
|
167
|
let row_index = index / (boundary_coord.y + 1);
|
151
|
168
|
let col_index = index % (boundary_coord.y + 1);
|
|
@@ -154,22 +171,22 @@ fn fill_grid_with_coordinate(
|
154
|
171
|
// South
|
155
|
172
|
if col_index < boundary_coord.y {
|
156
|
173
|
println!("south: {}", index + 1);
|
157
|
|
- // fill_grid_with_coordinate(grid, index + 1, coord, boundary_coord)?;
|
|
174
|
+ fill_grid_with_coordinate(grid, index + 1, coord, boundary_coord, iterations + 1)?;
|
158
|
175
|
}
|
159
|
176
|
// North
|
160
|
177
|
if col_index > 0 {
|
161
|
178
|
println!("north: {}", index - 1);
|
162
|
|
- // fill_grid_with_coordinate(grid, index - 1, coord, boundary_coord)?;
|
|
179
|
+ fill_grid_with_coordinate(grid, index - 1, coord, boundary_coord, iterations + 1)?;
|
163
|
180
|
}
|
164
|
181
|
// East
|
165
|
182
|
if row_index < boundary_coord.x {
|
166
|
183
|
println!("east: {}", index + (boundary_coord.y + 1));
|
167
|
|
- // fill_grid_with_coordinate(grid, index + (boundary_coord.y + 1), coord, boundary_coord)?;
|
|
184
|
+ fill_grid_with_coordinate(grid, index + (boundary_coord.y + 1), coord, boundary_coord, iterations + 1)?;
|
168
|
185
|
}
|
169
|
186
|
// West
|
170
|
187
|
if row_index > 0 {
|
171
|
188
|
println!("west: {}", index - (boundary_coord.y + 1));
|
172
|
|
- // fill_grid_with_coordinate(grid, index - (boundary_coord.y + 1), coord, boundary_coord)?;
|
|
189
|
+ fill_grid_with_coordinate(grid, index - (boundary_coord.y + 1), coord, boundary_coord, iterations + 1)?;
|
173
|
190
|
}
|
174
|
191
|
println!("returning grid");
|
175
|
192
|
Ok(grid)
|
|
@@ -270,7 +287,7 @@ mod tests {
|
270
|
287
|
}
|
271
|
288
|
|
272
|
289
|
#[test]
|
273
|
|
- fn fills_grid() {
|
|
290
|
+ fn fills_grid_with_one_coord() {
|
274
|
291
|
let boundary_coord = Coordinate { x: 1, y: 1 };
|
275
|
292
|
let mut grid = create_grid(boundary_coord);
|
276
|
293
|
let coord = Coordinate { x: 0, y: 0 };
|
|
@@ -304,4 +321,39 @@ mod tests {
|
304
|
321
|
]
|
305
|
322
|
);
|
306
|
323
|
}
|
|
324
|
+
|
|
325
|
+ #[test]
|
|
326
|
+ fn fills_grid_with_two_coords() {
|
|
327
|
+ let boundary_coord = Coordinate { x: 1, y: 1 };
|
|
328
|
+ let mut grid = create_grid(boundary_coord);
|
|
329
|
+ let coord_a = Coordinate { x: 0, y: 0 };
|
|
330
|
+ let coord_b = Coordinate { x: 1, y: 1 };
|
|
331
|
+ assert_eq!(
|
|
332
|
+ fill_grid(&mut grid, vec![coord_a, coord_b], boundary_coord).unwrap(),
|
|
333
|
+ &mut vec![
|
|
334
|
+ GridPoint::Filled {
|
|
335
|
+ x: 0,
|
|
336
|
+ y: 0,
|
|
337
|
+ closest_coord: coord_a,
|
|
338
|
+ closest_dist: 0,
|
|
339
|
+ },
|
|
340
|
+ GridPoint::Tied {
|
|
341
|
+ x: 0,
|
|
342
|
+ y: 1,
|
|
343
|
+ closest_dist: 1,
|
|
344
|
+ },
|
|
345
|
+ GridPoint::Tied {
|
|
346
|
+ x: 1,
|
|
347
|
+ y: 0,
|
|
348
|
+ closest_dist: 1,
|
|
349
|
+ },
|
|
350
|
+ GridPoint::Filled {
|
|
351
|
+ x: 1,
|
|
352
|
+ y: 1,
|
|
353
|
+ closest_coord: coord_b,
|
|
354
|
+ closest_dist: 2,
|
|
355
|
+ },
|
|
356
|
+ ]
|
|
357
|
+ );
|
|
358
|
+ }
|
307
|
359
|
}
|