Skip to content

Commit e006ac4

Browse files
committed
feat: Solving day 12
1 parent 4b0e792 commit e006ac4

File tree

3 files changed

+247
-0
lines changed

3 files changed

+247
-0
lines changed

docs/day12.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
url: "https://adventofcode.com/2024/day/12"
3+
---
4+
5+
# Day 12: Garden Groups
6+
7+
Why not search for the Chief Historian near the gardener and his massive farm? There's plenty of food, so The Historians grab something to eat while they search.
8+
9+
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 fences 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.
10+
11+
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:
12+
13+
```txt
14+
AAAA
15+
BBCD
16+
BBCC
17+
EEEC
18+
```
19+
20+
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.
21+
22+
In order to accurately calculate the cost of the fence around a single region, you need to know that region's area and perimeter.
23+
24+
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`.
25+
26+
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`.
27+
28+
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:
29+
30+
```txt
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+
```txt
49+
OOOOO
50+
OXOXO
51+
OOOOO
52+
OXOXO
53+
OOOOO
54+
```
55+
56+
The above map contains five regions, one containing all of the `O` garden plots, and the other four each containing a single `X` plot.
57+
58+
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`.
59+
60+
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.
61+
62+
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`.
63+
64+
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)`.
65+
66+
Here's a larger example:
67+
68+
```txt
69+
RRRRIICCFF
70+
RRRRIICCCF
71+
VVRRRCCFFF
72+
VVRCCCJFFF
73+
VVVVCJJCFE
74+
VVIVCCJJEE
75+
VVIIICJJEE
76+
MIIIIIJJEE
77+
MIIISIJEEE
78+
MMMISSJEEE
79+
```
80+
81+
It contains:
82+
83+
* A region of R plants with price `12 * 18 = 216`.
84+
* A region of I plants with price `4 * 8 = 32`.
85+
* A region of C plants with price `14 * 28 = 392`.
86+
* A region of F plants with price `10 * 18 = 180`.
87+
* A region of V plants with price `13 * 20 = 260`.
88+
* A region of J plants with price `11 * 20 = 220`.
89+
* A region of C plants with price `1 * 4 = 4`.
90+
* A region of E plants with price `13 * 18 = 234`.
91+
* A region of I plants with price `14 * 22 = 308`.
92+
* A region of M plants with price `5 * 12 = 60`.
93+
* A region of S plants with price `3 * 8 = 24`.
94+
95+
So, it has a total price of `1930`.
96+
97+
What is the total price of fencing all regions on your map?

src/day12/day12.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package day12
2+
3+
import (
4+
"strings"
5+
)
6+
7+
type point struct {
8+
x, y int
9+
}
10+
11+
type fence struct {
12+
x, y int
13+
direction rune
14+
}
15+
16+
func (p point) surroundingFences() []fence {
17+
top := fence{x: p.x, y: p.y, direction: '-'}
18+
bottom := fence{x: p.x, y: p.y + 1, direction: '-'}
19+
left := fence{x: p.x, y: p.y, direction: '|'}
20+
right := fence{x: p.x + 1, y: p.y, direction: '|'}
21+
return []fence{top, bottom, left, right}
22+
}
23+
24+
type patch struct {
25+
area uint
26+
perimeter uint
27+
}
28+
29+
func Solve(input string) uint {
30+
crops := parseInput(input)
31+
var cost uint = 0
32+
for _, crop := range crops {
33+
uniquePatches, fencesUsed := findUniquePatches(crop)
34+
details := patchDetails(uniquePatches, fencesUsed)
35+
for _, d := range details {
36+
cost += d.area * d.perimeter
37+
}
38+
}
39+
return cost
40+
}
41+
42+
func parseInput(input string) map[rune][]point {
43+
crops := make(map[rune][]point)
44+
for j, line := range strings.Split(input, "\n") {
45+
for i, plant := range line {
46+
crops[plant] = append(crops[plant], point{x: i, y: j})
47+
}
48+
}
49+
return crops
50+
}
51+
52+
func findUniquePatches(crop []point) (map[uint][]point, map[fence]uint) {
53+
var nextPatchId uint = 1
54+
patches := make(map[uint][]point)
55+
fencesByPatchId := make(map[fence]uint)
56+
for _, p := range crop {
57+
usedNewPatchId := true
58+
currentPatchId := nextPatchId
59+
newlyUsedFences := make([]fence, 0)
60+
61+
for _, f := range p.surroundingFences() {
62+
if fencesByPatchId[f] == 0 {
63+
newlyUsedFences = append(newlyUsedFences, f)
64+
continue
65+
}
66+
67+
if fencesByPatchId[f] != currentPatchId {
68+
patches[fencesByPatchId[f]] = append(patches[fencesByPatchId[f]], patches[currentPatchId]...)
69+
delete(patches, currentPatchId)
70+
currentPatchId = fencesByPatchId[f]
71+
usedNewPatchId = false
72+
}
73+
delete(fencesByPatchId, f)
74+
}
75+
76+
for _, f := range newlyUsedFences {
77+
fencesByPatchId[f] = currentPatchId
78+
}
79+
80+
patches[currentPatchId] = append(patches[currentPatchId], p)
81+
82+
if usedNewPatchId {
83+
nextPatchId++
84+
}
85+
}
86+
87+
return patches, fencesByPatchId
88+
}
89+
90+
func patchDetails(patches map[uint][]point, fencesUsed map[fence]uint) []patch {
91+
details := make([]patch, len(patches))
92+
i := 0
93+
for _, points := range patches {
94+
details[i].area = uint(len(points))
95+
for _, p := range points {
96+
for _, f := range p.surroundingFences() {
97+
if fencesUsed[f] > 0 {
98+
details[i].perimeter++
99+
}
100+
}
101+
}
102+
i++
103+
}
104+
return details
105+
}

src/day12/day12_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package day12
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestSample(t *testing.T) {
8+
input := `AAAA
9+
BBCD
10+
BBCC
11+
EEEC`
12+
result := Solve(input)
13+
if result != 140 {
14+
t.Errorf("Calculated solution was not expected")
15+
}
16+
}
17+
18+
func TestSample2(t *testing.T) {
19+
input := `OOOOO
20+
OXOXO
21+
OOOOO
22+
OXOXO
23+
OOOOO`
24+
result := Solve(input)
25+
if result != 772 {
26+
t.Errorf("Calculated solution was not expected")
27+
}
28+
}
29+
30+
func TestSample3(t *testing.T) {
31+
input := `RRRRIICCFF
32+
RRRRIICCCF
33+
VVRRRCCFFF
34+
VVRCCCJFFF
35+
VVVVCJJCFE
36+
VVIVCCJJEE
37+
VVIIICJJEE
38+
MIIIIIJJEE
39+
MIIISIJEEE
40+
MMMISSJEEE`
41+
result := Solve(input)
42+
if result != 1930 {
43+
t.Errorf("Calculated solution was not expected")
44+
}
45+
}

0 commit comments

Comments
 (0)