|
| 1 | +# Day 12: Garden Groups |
| 2 | + |
| 3 | +[https://adventofcode.com/2024/day/12](https://adventofcode.com/2024/day/12) |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +### Part One |
| 8 | + |
| 9 | +Why not search for the Chief Historian near the [gardener](https://adventofcode.com/2023/day/5) and his [massive farm](https://adventofcode.com/2023/day/21)? There's plenty of food, so The Historians grab something to eat while they search. |
| 10 | + |
| 11 | +You're about to settle near a complex arrangement of garden plots when some Elves ask if you can lend a hand. They'd like to set up <span title="I originally wanted to title this puzzle "Fencepost Problem", but I was afraid someone would then try to count fenceposts by mistake and experience a fencepost problem.">fences</span> around each region of garden plots, but they can't figure out how much fence they need to order or how much it will cost. They hand you a map (your puzzle input) of the garden plots. |
| 12 | + |
| 13 | +Each garden plot grows only a single type of plant and is indicated by a single letter on your map. When multiple garden plots are growing the same type of plant and are touching (horizontally or vertically), they form a _region_. For example: |
| 14 | + |
| 15 | + AAAA |
| 16 | + BBCD |
| 17 | + BBCC |
| 18 | + EEEC |
| 19 | + |
| 20 | + |
| 21 | +This 4x4 arrangement includes garden plots growing five different types of plants (labeled `A`, `B`, `C`, `D`, and `E`), each grouped into their own region. |
| 22 | + |
| 23 | +In order to accurately calculate the cost of the fence around a single region, you need to know that region's _area_ and _perimeter_. |
| 24 | + |
| 25 | +The _area_ of a region is simply the number of garden plots the region contains. The above map's type `A`, `B`, and `C` plants are each in a region of area `4`. The type `E` plants are in a region of area `3`; the type `D` plants are in a region of area `1`. |
| 26 | + |
| 27 | +Each garden plot is a square and so has _four sides_. The _perimeter_ of a region is the number of sides of garden plots in the region that do not touch another garden plot in the same region. The type `A` and `C` plants are each in a region with perimeter `10`. The type `B` and `E` plants are each in a region with perimeter `8`. The lone `D` plot forms its own region with perimeter `4`. |
| 28 | + |
| 29 | +Visually indicating the sides of plots in each region that contribute to the perimeter using `-` and `|`, the above map's regions' perimeters are measured as follows: |
| 30 | + |
| 31 | + +-+-+-+-+ |
| 32 | + |A A A A| |
| 33 | + +-+-+-+-+ +-+ |
| 34 | + |D| |
| 35 | + +-+-+ +-+ +-+ |
| 36 | + |B B| |C| |
| 37 | + + + + +-+ |
| 38 | + |B B| |C C| |
| 39 | + +-+-+ +-+ + |
| 40 | + |C| |
| 41 | + +-+-+-+ +-+ |
| 42 | + |E E E| |
| 43 | + +-+-+-+ |
| 44 | + |
| 45 | + |
| 46 | +Plants of the same type can appear in multiple separate regions, and regions can even appear within other regions. For example: |
| 47 | + |
| 48 | + OOOOO |
| 49 | + OXOXO |
| 50 | + OOOOO |
| 51 | + OXOXO |
| 52 | + OOOOO |
| 53 | + |
| 54 | + |
| 55 | +The above map contains _five_ regions, one containing all of the `O` garden plots, and the other four each containing a single `X` plot. |
| 56 | + |
| 57 | +The four `X` regions each have area `1` and perimeter `4`. The region containing `21` type `O` plants is more complicated; in addition to its outer edge contributing a perimeter of `20`, its boundary with each `X` region contributes an additional `4` to its perimeter, for a total perimeter of `36`. |
| 58 | + |
| 59 | +Due to "modern" business practices, the _price_ of fence required for a region is found by _multiplying_ that region's area by its perimeter. The _total price_ of fencing all regions on a map is found by adding together the price of fence for every region on the map. |
| 60 | + |
| 61 | +In the first example, region `A` has price `4 * 10 = 40`, region `B` has price `4 * 8 = 32`, region `C` has price `4 * 10 = 40`, region `D` has price `1 * 4 = 4`, and region `E` has price `3 * 8 = 24`. So, the total price for the first example is _`140`_. |
| 62 | + |
| 63 | +In the second example, the region with all of the `O` plants has price `21 * 36 = 756`, and each of the four smaller `X` regions has price `1 * 4 = 4`, for a total price of _`772`_ (`756 + 4 + 4 + 4 + 4`). |
| 64 | + |
| 65 | +Here's a larger example: |
| 66 | + |
| 67 | + RRRRIICCFF |
| 68 | + RRRRIICCCF |
| 69 | + VVRRRCCFFF |
| 70 | + VVRCCCJFFF |
| 71 | + VVVVCJJCFE |
| 72 | + VVIVCCJJEE |
| 73 | + VVIIICJJEE |
| 74 | + MIIIIIJJEE |
| 75 | + MIIISIJEEE |
| 76 | + MMMISSJEEE |
| 77 | + |
| 78 | + |
| 79 | +It contains: |
| 80 | + |
| 81 | +* A region of `R` plants with price `12 * 18 = 216`. |
| 82 | +* A region of `I` plants with price `4 * 8 = 32`. |
| 83 | +* A region of `C` plants with price `14 * 28 = 392`. |
| 84 | +* A region of `F` plants with price `10 * 18 = 180`. |
| 85 | +* A region of `V` plants with price `13 * 20 = 260`. |
| 86 | +* A region of `J` plants with price `11 * 20 = 220`. |
| 87 | +* A region of `C` plants with price `1 * 4 = 4`. |
| 88 | +* A region of `E` plants with price `13 * 18 = 234`. |
| 89 | +* A region of `I` plants with price `14 * 22 = 308`. |
| 90 | +* A region of `M` plants with price `5 * 12 = 60`. |
| 91 | +* A region of `S` plants with price `3 * 8 = 24`. |
| 92 | + |
| 93 | +So, it has a total price of _`1930`_. |
| 94 | + |
| 95 | +_What is the total price of fencing all regions on your map?_ |
0 commit comments