-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_12.rs
574 lines (502 loc) · 17.1 KB
/
day_12.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
///
/// # day_12.rs
/// Code for the day 12 of the Advent of Code challenge year 2024
///
/// This code solves a problem involving calculating prices for different regions in a garden plot.
/// The garden is represented as a grid where each character represents a different type of plant.
/// Adjacent cells with the same plant type form regions.
///
/// ## Part 1
/// Calculates region prices based on:
/// - Area (number of cells in the region)
/// - Perimeter (number of edges adjacent to different plant types or garden borders)
///
/// Price = Area × Perimeter
///
/// ## Part 2
/// Uses a different pricing formula based on:
/// - Area (number of cells in the region)
/// - Number of distinct sides (continuous boundaries, counting both outer and inner edges)
///
/// Price = Area × Number of Sides
///
/// ## Implementation Details
/// - Uses flood fill algorithm to identify connected regions
/// - Implements boundary detection for perimeter calculation
/// - Uses HashSets for efficient boundary cell tracking
/// - Handles complex cases including:
/// * Regions with holes
/// * Nested regions
/// * Irregular shapes
///
/// ## Key Components
/// - Garden struct: Represents the garden grid and contains all processing methods
/// - find_regions: Identifies all distinct plant regions
/// - calculate_perimeter: Counts edges for part 1 pricing
/// - calculate_sides: Counts distinct boundaries for part 2 pricing
/// - flood_fill: Recursive algorithm for region detection
///
// Imports ============================================================================== Imports
use std::{collections::HashSet, ops::Add, str::FromStr};
use aoc_2024::Direction;
// Variables =========================================================================== Variables
const INPUT: &str = include_str!("../../data/inputs/day_12.txt");
#[derive(Debug)]
struct Garden {
grid: Vec<Vec<char>>,
height: usize,
width: usize,
}
impl FromStr for Garden {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let grid: Vec<Vec<char>> = s
.trim()
.lines()
.map(|line| line.chars().collect())
.collect();
let height = grid.len();
let width = if height > 0 { grid[0].len() } else { 0 };
Ok(Garden {
grid,
height,
width,
})
}
}
impl Garden {
///
/// # `find_regions`
/// Find all regions of the garden.
/// A region is a group of adjacent cells with the same plant type.
///
/// ## Returns
/// * `Vec<Vec<(usize, usize)>>` - A vector of regions, where each region is a vector of coordinates of the cells in the region
fn find_regions(&self) -> Vec<Vec<(usize, usize)>> {
let mut visited = vec![vec![false; self.width]; self.height];
let mut regions = Vec::new();
for y in 0..self.height {
for x in 0..self.width {
if !visited[y][x] {
let mut region = Vec::new();
self.flood_fill(x, y, self.grid[y][x], &mut visited, &mut region);
if !region.is_empty() {
regions.push(region);
}
}
}
}
regions
}
///
/// # `flood_fill`
/// Recursive function to fill a region of the garden with a plant type.
///
/// ## Arguments
/// * `x` - The x coordinate of the cell
/// * `y` - The y coordinate of the cell
/// * `plant_type` - The type of plant to fill the region with
/// * `visited` - A 2D vector of booleans to keep track of visited cells
/// * `region` - A vector of coordinates of the cells in the region
fn flood_fill(
&self,
x: usize,
y: usize,
plant_type: char,
visited: &mut Vec<Vec<bool>>,
region: &mut Vec<(usize, usize)>, // Now stores (y, x)
) {
if visited[y][x] || self.grid[y][x] != plant_type {
return;
}
visited[y][x] = true;
region.push((y, x)); // Changed from (x, y) to (y, x)
// Check all four adjacent cells
let neighbors = [
(x, y).add(aoc_2024::Direction::Left),
(x, y).add(aoc_2024::Direction::Right),
(x, y).add(aoc_2024::Direction::Down),
(x, y).add(aoc_2024::Direction::Up),
];
for (nx, ny) in neighbors {
if nx < self.width && ny < self.height {
self.flood_fill(nx, ny, plant_type, visited, region);
}
}
}
///
/// # `calculate_perimeter`
/// Calculate the perimeter of a region.
/// The perimeter is the number of cells that are adjacent to a cell of a different plant type.
///
/// ## Arguments
/// * `region` - A vector of coordinates of the cells in the region
///
/// ## Returns
/// * `u64` - The perimeter of the region
fn calculate_region_price(&self, region: &[(usize, usize)]) -> u64 {
let area = region.len() as u64;
let perimeter = self.calculate_perimeter(region);
area * perimeter
}
///
/// # `calculate_perimeter`
/// Calculate the perimeter of a region.
/// The perimeter is the number of cells that are adjacent to a cell of a different plant type.
///
/// ## Arguments
/// * `region` - A vector of coordinates of the cells in the region
///
/// ## Returns
/// * `u64` - The perimeter of the region
fn calculate_perimeter(&self, region: &[(usize, usize)]) -> u64 {
let mut perimeter = 0;
let region_set: std::collections::HashSet<_> = region.iter().cloned().collect();
for &(x, y) in region {
// Check all four sides of the current cell
let neighbors = [
(x, y).add(aoc_2024::Direction::Left), // left
(x, y).add(aoc_2024::Direction::Right), // right
(x, y).add(aoc_2024::Direction::Down), // down
(x, y).add(aoc_2024::Direction::Up), // up
];
for (nx, ny) in neighbors {
// A side contributes to perimeter if it's:
// 1. On the edge of the grid, or
// 2. Adjacent to a different plant type
if nx >= self.width || ny >= self.height || !region_set.contains(&(nx, ny)) {
perimeter += 1;
}
}
}
perimeter
}
///
/// # `calculate_sides`
/// Calculate the number of distinct sides of a region.
/// A side is counted as one unit regardless of its length.
/// For regions with holes, both inner and outer sides are counted.
///
/// ## Arguments
/// * `region` - A vector of coordinates of the cells in the region
///
/// ## Returns
/// * `u64` - The number of distinct sides in the region
fn calculate_sides(&self, region: &[(usize, usize)]) -> u64 {
let region_cells: HashSet<(usize, usize)> = region.iter().cloned().collect();
let boundary_cells = self.find_boundary_cells(region, ®ion_cells);
let continuous_boundaries = self.group_boundaries_by_direction(boundary_cells);
continuous_boundaries.len() as u64
}
///
/// # `find_boundary_cells`
/// Finds all cells that form the boundary of a region.
/// A boundary cell is adjacent to the region but not part of it.
///
/// ## Arguments
/// * `region` - The cells that make up the region
/// * `region_cells` - HashSet of the region cells for efficient lookup
///
/// ## Returns
/// * A HashSet of ((x, y), direction) pairs representing boundary cells and their direction relative to the region
fn find_boundary_cells(
&self,
region: &[(usize, usize)],
region_cells: &HashSet<(usize, usize)>,
) -> HashSet<((usize, usize), Direction)> {
let directions = [
Direction::Up,
Direction::Right,
Direction::Down,
Direction::Left,
];
region
.iter()
.flat_map(|&cell| {
directions.iter().filter_map(move |&direction| {
let adjacent_cell = cell.add(direction);
let (x, y) = adjacent_cell;
// Check if the adjacent cell is outside the region
if x >= self.width || y >= self.height || !region_cells.contains(&adjacent_cell)
{
Some((adjacent_cell, direction))
} else {
None
}
})
})
.collect()
}
///
/// # `group_boundaries_by_direction`
/// Groups boundary cells into continuous boundaries based on their direction.
/// Each continuous boundary represents one "side" of the region.
///
/// ## Arguments
/// * `boundary_cells` - Set of boundary cells with their directions
///
/// ## Returns
/// * Vector of HashSets, where each HashSet contains the cells forming one continuous boundary
fn group_boundaries_by_direction(
&self,
boundary_cells: HashSet<((usize, usize), Direction)>,
) -> Vec<HashSet<(usize, usize)>> {
let directions = [
Direction::Up,
Direction::Right,
Direction::Down,
Direction::Left,
];
let mut continuous_boundaries = Vec::new();
for direction in directions {
// Get all boundary cells for the current direction
let mut direction_cells: HashSet<(usize, usize)> = boundary_cells
.iter()
.filter(|(_, dir)| *dir == direction)
.map(|((x, y), _)| (*x, *y))
.collect();
// Process cells until we've found all continuous boundaries in this direction
while !direction_cells.is_empty() {
if let Some(boundary) = self.extract_continuous_boundary(&mut direction_cells) {
continuous_boundaries.push(boundary);
}
}
}
continuous_boundaries
}
///
/// # `extract_continuous_boundary`
/// Extracts a single continuous boundary from a set of cells.
/// Uses flood-fill algorithm to find all connected cells.
///
/// ## Arguments
/// * `remaining_cells` - Set of unprocessed boundary cells
///
/// ## Returns
/// * Option<HashSet<(usize, usize)>> - The extracted continuous boundary, if any
fn extract_continuous_boundary(
&self,
remaining_cells: &mut HashSet<(usize, usize)>,
) -> Option<HashSet<(usize, usize)>> {
let &start_cell = remaining_cells.iter().next()?;
let mut continuous_boundary = HashSet::new();
let mut cells_to_check = vec![start_cell];
let directions = [
Direction::Up,
Direction::Right,
Direction::Down,
Direction::Left,
];
while let Some(current_cell) = cells_to_check.pop() {
if continuous_boundary.insert(current_cell) {
remaining_cells.remove(¤t_cell);
// Check adjacent cells
for &direction in &directions {
let adjacent_cell = current_cell.add(direction);
if remaining_cells.contains(&adjacent_cell) {
cells_to_check.push(adjacent_cell);
}
}
}
}
Some(continuous_boundary)
}
///
/// # `calculate_region_price_part2`
/// Calculate the price of a region for part 2.
///
/// ## Arguments
/// * `region` - A vector of coordinates of the cells in the region
///
/// ## Returns
/// * `u64` - The price of the region
fn calculate_region_price_part_2(&self, region: &[(usize, usize)]) -> u64 {
let area = region.len() as u64;
let sides = self.calculate_sides(region);
area * sides
}
}
// Functions =========================================================================== Functions
pub fn response_part_1() {
println!("Day 12 - Part 1");
let start = std::time::Instant::now();
let garden: Garden = INPUT.parse().unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price(region))
.sum();
let duration = start.elapsed();
println!("Total price: {}", total_price);
println!("Duration: {duration:?}");
}
pub fn response_part_2() {
println!("Day 12 - Part 2");
let start = std::time::Instant::now();
let garden: Garden = INPUT.parse().unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price_part_2(region))
.sum();
let duration = start.elapsed();
println!("Total price: {}", total_price);
println!("Duration: {duration:?}");
}
fn main() {
response_part_1();
response_part_2();
}
// Tests ==================================================================================== Tests
#[cfg(test)]
mod tests {
use super::*;
const SIMPLE_EXAMPLE: &str = "\
AAAA
BBCD
BBCC
EEEC";
const XOXO_EXAMPLE: &str = "\
OOOOO
OXOXO
OOOOO
OXOXO
OOOOO";
const EXAMPLE: &str = "
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE";
const LARGE_EXAMPLE: &str = "\
RRRRIICCFF
RRRRIICCCF
VVRRRCCFFF
VVRCCCJFFF
VVVVCJJCFE
VVIVCCJJEE
VVIIICJJEE
MIIIIIJJEE
MIIISIJEEE
MMMISSJEEE";
#[test]
fn test_from_str() {
let garden = Garden::from_str(EXAMPLE).unwrap();
assert_eq!(garden.height, 10);
assert_eq!(garden.width, 10);
}
#[test]
fn test_simple_garden() {
let garden: Garden = SIMPLE_EXAMPLE.parse().unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price(region))
.sum();
assert_eq!(total_price, 140);
}
#[test]
fn test_nested_regions() {
let garden = Garden::from_str(XOXO_EXAMPLE).unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price(region))
.sum();
assert_eq!(total_price, 772);
}
#[test]
fn test_larger_garden() {
let garden = Garden::from_str(LARGE_EXAMPLE).unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price(region))
.sum();
assert_eq!(total_price, 1930);
}
#[test]
fn test_calculate_sides() {
let garden = Garden::from_str(SIMPLE_EXAMPLE).unwrap();
let regions = garden.find_regions();
println!("{:?}", regions);
let a_sides = garden.calculate_sides(®ions[0]);
let b_sides = garden.calculate_sides(®ions[1]);
let c_sides = garden.calculate_sides(®ions[2]);
let d_sides = garden.calculate_sides(®ions[3]);
let e_sides = garden.calculate_sides(®ions[4]);
assert_eq!(a_sides, 4);
assert_eq!(b_sides, 4);
assert_eq!(c_sides, 8);
assert_eq!(d_sides, 4);
assert_eq!(e_sides, 4);
}
#[test]
fn test_simple_garden_part_2() {
let garden: Garden = SIMPLE_EXAMPLE.parse().unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price_part_2(region))
.sum();
assert_eq!(total_price, 80);
}
#[test]
fn test_nested_regions_part_2() {
let garden = Garden::from_str(XOXO_EXAMPLE).unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price_part_2(region))
.sum();
assert_eq!(total_price, 436);
}
#[test]
fn test_part_2_ex_1() {
let input = "\
EEEEE
EXXXX
EEEEE
EXXXX
EEEEE";
let garden = Garden::from_str(input).unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price_part_2(region))
.sum();
assert_eq!(total_price, 236);
}
#[test]
fn test_part_2_ex_2() {
let input = "\
AAAAAA
AAABBA
AAABBA
ABBAAA
ABBAAA
AAAAAA";
let garden = Garden::from_str(input).unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price_part_2(region))
.sum();
assert_eq!(total_price, 368);
}
#[test]
fn test_larger_garden_part_2() {
let garden = Garden::from_str(LARGE_EXAMPLE).unwrap();
let regions = garden.find_regions();
let total_price: u64 = regions
.iter()
.map(|region| garden.calculate_region_price_part_2(region))
.sum();
assert_eq!(total_price, 1206);
}
}