|
@@ -10,7 +10,7 @@ use regex::{Regex, Captures};
|
10
|
10
|
|
11
|
11
|
const INPUT: &str = "inputs/6.txt";
|
12
|
12
|
|
13
|
|
-#[derive(Debug, PartialEq)]
|
|
13
|
+#[derive(Debug, PartialEq, Copy, Clone)]
|
14
|
14
|
struct Coordinate {
|
15
|
15
|
x: u32,
|
16
|
16
|
y: u32,
|
|
@@ -113,36 +113,67 @@ fn create_grid(boundary_coord: Coordinate) -> Vec<GridPoint> {
|
113
|
113
|
grid
|
114
|
114
|
}
|
115
|
115
|
|
116
|
|
-// fn fill_grid(
|
117
|
|
- // grid: &mut Vec<GridPoint>,
|
118
|
|
- // coords: Vec<Coordinate>,
|
119
|
|
- // boundary_coord: Coordinate,
|
120
|
|
-// ) -> &mut Vec<GridPoint> {
|
121
|
|
- // for coord in coords {
|
122
|
|
- // fill_grid_with_coordinate(grid, coord, boundary_coord);
|
123
|
|
- // }
|
124
|
|
- // grid
|
125
|
|
-// }
|
126
|
|
-
|
127
|
|
-// fn fill_grid_with_coordinate(
|
128
|
|
- // grid: &mut Vec<GridPoint>,
|
129
|
|
- // point: GridPoint,
|
130
|
|
- // coord: Coordinate,
|
131
|
|
- // boundary_coord: Coordinate,
|
132
|
|
-// ) -> &mut Vec<GridPoint> {
|
133
|
|
- // match point {
|
134
|
|
- // GridPoint::Unfilled { x, y } => {
|
135
|
|
- // mem::replace(
|
136
|
|
- // &mut grid[x + ((boundary_coord.y + 1) * y)],
|
137
|
|
- // GridPoint::Filled {
|
138
|
|
- // x: x,
|
139
|
|
- // y: y,
|
140
|
|
- // closest_coord: coord,
|
141
|
|
- // closest_dist: manhattan_dist(coord.x, coord.y, x, y),
|
142
|
|
- // });
|
143
|
|
- // }
|
144
|
|
- // }
|
145
|
|
-// }
|
|
116
|
+fn fill_grid(
|
|
117
|
+ grid: &mut Vec<GridPoint>,
|
|
118
|
+ coords: Vec<Coordinate>,
|
|
119
|
+ boundary_coord: Coordinate,
|
|
120
|
+) -> Result<&mut Vec<GridPoint>, Box<Error>> {
|
|
121
|
+ for coord in coords {
|
|
122
|
+ let start_index = (coord.x * (boundary_coord.y + 1)) + coord.y;
|
|
123
|
+ fill_grid_with_coordinate(grid, start_index, coord, boundary_coord)?;
|
|
124
|
+ }
|
|
125
|
+ Ok(grid)
|
|
126
|
+}
|
|
127
|
+
|
|
128
|
+fn fill_grid_with_coordinate(
|
|
129
|
+ grid: &mut Vec<GridPoint>,
|
|
130
|
+ index: u32,
|
|
131
|
+ coord: Coordinate,
|
|
132
|
+ boundary_coord: Coordinate,
|
|
133
|
+) -> Result<&mut Vec<GridPoint>, Box<Error>> {
|
|
134
|
+ println!("index: {}", index);
|
|
135
|
+ let point = &mut grid.get(index as usize).unwrap();
|
|
136
|
+ match point {
|
|
137
|
+ GridPoint::Unfilled { x, y } => {
|
|
138
|
+ mem::replace(
|
|
139
|
+ point,
|
|
140
|
+ &GridPoint::Filled {
|
|
141
|
+ x: *x,
|
|
142
|
+ y: *y,
|
|
143
|
+ closest_coord: coord,
|
|
144
|
+ closest_dist: manhattan_dist(coord.x, coord.y, *x, *y),
|
|
145
|
+ });
|
|
146
|
+ },
|
|
147
|
+ GridPoint::Tied { x: _, y: _, closest_dist: _ } => {},
|
|
148
|
+ GridPoint::Filled { x: _, y: _, closest_coord: _, closest_dist: _ } => {},
|
|
149
|
+ }
|
|
150
|
+ let row_index = index / (boundary_coord.y + 1);
|
|
151
|
+ let col_index = index % (boundary_coord.y + 1);
|
|
152
|
+ println!("row_index: {}", row_index);
|
|
153
|
+ println!("col_index: {}", col_index);
|
|
154
|
+ // South
|
|
155
|
+ if col_index < boundary_coord.y {
|
|
156
|
+ println!("south: {}", index + 1);
|
|
157
|
+ // fill_grid_with_coordinate(grid, index + 1, coord, boundary_coord)?;
|
|
158
|
+ }
|
|
159
|
+ // North
|
|
160
|
+ if col_index > 0 {
|
|
161
|
+ println!("north: {}", index - 1);
|
|
162
|
+ // fill_grid_with_coordinate(grid, index - 1, coord, boundary_coord)?;
|
|
163
|
+ }
|
|
164
|
+ // East
|
|
165
|
+ if row_index < boundary_coord.x {
|
|
166
|
+ println!("east: {}", index + (boundary_coord.y + 1));
|
|
167
|
+ // fill_grid_with_coordinate(grid, index + (boundary_coord.y + 1), coord, boundary_coord)?;
|
|
168
|
+ }
|
|
169
|
+ // West
|
|
170
|
+ if row_index > 0 {
|
|
171
|
+ println!("west: {}", index - (boundary_coord.y + 1));
|
|
172
|
+ // fill_grid_with_coordinate(grid, index - (boundary_coord.y + 1), coord, boundary_coord)?;
|
|
173
|
+ }
|
|
174
|
+ println!("returning grid");
|
|
175
|
+ Ok(grid)
|
|
176
|
+}
|
146
|
177
|
|
147
|
178
|
fn manhattan_dist(x1: u32, y1: u32, x2: u32, y2: u32) -> u32 {
|
148
|
179
|
((x2 as i32 - x1 as i32) + (y2 as i32 - y1 as i32)).abs() as u32
|
|
@@ -237,4 +268,40 @@ mod tests {
|
237
|
268
|
assert_eq!(manhattan_dist(0, 0, 0, 0), 0);
|
238
|
269
|
assert_eq!(manhattan_dist(2, 1, 0, 0), 3);
|
239
|
270
|
}
|
|
271
|
+
|
|
272
|
+ #[test]
|
|
273
|
+ fn fills_grid() {
|
|
274
|
+ let boundary_coord = Coordinate { x: 1, y: 1 };
|
|
275
|
+ let mut grid = create_grid(boundary_coord);
|
|
276
|
+ let coord = Coordinate { x: 0, y: 0 };
|
|
277
|
+ assert_eq!(
|
|
278
|
+ fill_grid(&mut grid, vec![coord], boundary_coord).unwrap(),
|
|
279
|
+ &mut vec![
|
|
280
|
+ GridPoint::Filled {
|
|
281
|
+ x: 0,
|
|
282
|
+ y: 0,
|
|
283
|
+ closest_coord: coord,
|
|
284
|
+ closest_dist: 0,
|
|
285
|
+ },
|
|
286
|
+ GridPoint::Filled {
|
|
287
|
+ x: 0,
|
|
288
|
+ y: 1,
|
|
289
|
+ closest_coord: coord,
|
|
290
|
+ closest_dist: 1,
|
|
291
|
+ },
|
|
292
|
+ GridPoint::Filled {
|
|
293
|
+ x: 1,
|
|
294
|
+ y: 0,
|
|
295
|
+ closest_coord: coord,
|
|
296
|
+ closest_dist: 1,
|
|
297
|
+ },
|
|
298
|
+ GridPoint::Filled {
|
|
299
|
+ x: 1,
|
|
300
|
+ y: 1,
|
|
301
|
+ closest_coord: coord,
|
|
302
|
+ closest_dist: 2,
|
|
303
|
+ },
|
|
304
|
+ ]
|
|
305
|
+ );
|
|
306
|
+ }
|
240
|
307
|
}
|