--- Day 12: Garden Groups ---
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.
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.
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:
AAAA BBCD BBCC EEEC
This 4x4 arrangement includes garden plots growing five different types of plants (labeled
A
,B
,C
,D
, andE
), each grouped into their own region.In order to accurately calculate the cost of the fence around a single region, you need to know that region's area and perimeter.
The area of a region is simply the number of garden plots the region contains. The above map's type
A
,B
, andC
plants are each in a region of area4
. The typeE
plants are in a region of area3
; the typeD
plants are in a region of area1
.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
andC
plants are each in a region with perimeter10
. The typeB
andE
plants are each in a region with perimeter8
. The loneD
plot forms its own region with perimeter4
.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:+-+-+-+-+ |A A A A| +-+-+-+-+ +-+ |D| +-+-+ +-+ +-+ |B B| |C| + + + +-+ |B B| |C C| +-+-+ +-+ + |C| +-+-+-+ +-+ |E E E| +-+-+-+
Plants of the same type can appear in multiple separate regions, and regions can even appear within other regions. For example:
OOOOO OXOXO OOOOO OXOXO OOOOO
The above map contains five regions, one containing all of the
O
garden plots, and the other four each containing a singleX
plot.The four
X
regions each have area1
and perimeter4
. The region containing21
typeO
plants is more complicated; in addition to its outer edge contributing a perimeter of20
, its boundary with eachX
region contributes an additional4
to its perimeter, for a total perimeter of36
.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.
In the first example, region
A
has price4 * 10 = 40
, regionB
has price4 * 8 = 32
, regionC
has price4 * 10 = 40
, regionD
has price1 * 4 = 4
, and regionE
has price3 * 8 = 24
. So, the total price for the first example is140
.In the second example, the region with all of the
O
plants has price21 * 36 = 756
, and each of the four smallerX
regions has price1 * 4 = 4
, for a total price of772
(756 + 4 + 4 + 4 + 4
).Here's a larger example:
RRRRIICCFF RRRRIICCCF VVRRRCCFFF VVRCCCJFFF VVVVCJJCFE VVIVCCJJEE VVIIICJJEE MIIIIIJJEE MIIISIJEEE MMMISSJEEE
It contains:
- A region of
R
plants with price12 * 18 = 216
.- A region of
I
plants with price4 * 8 = 32
.- A region of
C
plants with price14 * 28 = 392
.- A region of
F
plants with price10 * 18 = 180
.- A region of
V
plants with price13 * 20 = 260
.- A region of
J
plants with price11 * 20 = 220
.- A region of
C
plants with price1 * 4 = 4
.- A region of
E
plants with price13 * 18 = 234
.- A region of
I
plants with price14 * 22 = 308
.- A region of
M
plants with price5 * 12 = 60
.- A region of
S
plants with price3 * 8 = 24
.So, it has a total price of
1930
.What is the total price of fencing all regions on your map?
n: count i: read0 `12.txt
We need to identify each group. First of all, let's write a function that returns all 4 "shifts" of a matrix: left, right, up, and down.
We can use the functions prev and next to do this.
(prev';next';prev;next) @\: 3 3#.Q.A " AB" " DE" " GH" "BC " "EF " "HI " "" "ABC" "DEF" "DEF" "GHI" ""
This is nearly correct, but has empty arrays for some values instead of lists of the original length.
We can use reshape to fix this.
3#''(prev';next';prev;next) @\: 3 3#.Q.A " AB" " DE" " GH" "BC " "EF " "HI " " " "ABC" "DEF" "DEF" "GHI" " "
Save this as a function.
shifts:{count[x]#''(prev';next';prev;next)@\:x}
Now, find the shifts of our grid, and check if it is equal to the grid. This makes 4 masks where the adjacent elements are equal.
show adj: i=/:shifts i 0111010101b 0111010110b 0101101011b 0100110011b 0111001000b 0100010101b 01011.. 1110101010b 1110101100b 1011010110b 1001100110b 1110010000b 1000101010b 10110.. 0000000000b 1111111101b 0011001001b 1110010111b 1100101010b 1101101001b 11100.. 1111111101b 0011001001b 1110010111b 1100101010b 1101101001b 1110011111b 00111..
Now, we can label each square with an id.
At first, the ids are all unique, and then we iteratively update them to the smallest of their neighbours.
This will find all contiguous in the grid.
show ids: (2#n)#til n*n 0 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
We find the minimum of our neighbours by shifting the ids, adding a large value where they're not adjacent, and then finding the minimum.
{x &/ (999999*not adj)+0^shifts x} ids 0 0 1 2 4 4 6 6 8 8 0 1 2 3 4 5 6 7 17 9 20 20 12 13 23 25 16 27 27 19 20 21 22 33 33 25 36 27 28 29 30 31 41 42 34 45 36 47 38 49 40 41 52 43 44 54 46 56 58 49 50 51 52 62 63 55 56 57 58 59 70 71 62 63 64 74 66 67 68 69 70 71 72 73 84 75 76 87 78 79 80 90 91 83 84 94 86 87 88 89
We can see this has updated some of the values. Now, if we do it until it converges, we should locate distinct groups.
show g:{x &/ (999999*not adj)+0^shifts x} over ids 0 0 0 0 4 4 6 6 8 8 0 0 0 0 4 4 6 6 6 8 20 20 0 0 0 6 6 8 8 8 20 20 0 6 6 6 36 8 8 8 20 20 20 20 6 36 36 47 8 49 20 20 52 20 6 6 36 36 49 49 20 20 52 52 52 6 36 36 49 49 70 52 52 52 52 52 36 36 49 49 70 52 52 52 84 52 36 49 49 49 70 70 70 52 84 84 36 49 49 49
Now we need to find the area, and perimeter. The area is simply the number of points, so can easily be calculated with group.
show A: count each group raze g 0 | 12 4 | 4 6 | 14 8 | 10 20| 13 36| 11 47| 1 49| 13 52| 14 70| 5 84| 3
The perimeter is slightly more complicated, but we can realise that a side that contributes to the perimeter is simply where any two squares are part of different groups, in any direction.
So we can calculate this using our shifts function from before.
(sum g <>/: shifts g) 2 1 1 2 2 2 2 2 3 2 2 1 0 1 2 2 1 1 3 2 2 2 1 1 3 2 2 2 1 1 1 1 3 3 1 2 3 2 0 2 1 0 2 2 2 3 1 4 3 3 1 1 3 3 2 2 1 2 2 1 2 2 1 1 2 3 1 1 1 1 3 2 0 0 1 2 1 2 1 1 2 2 1 1 3 3 2 2 0 1 2 2 3 3 2 3 3 2 1 2
This is the number of edges for each square.
show P: sum each (raze sum g <>/: shifts g) group raze g 0 | 18 4 | 8 6 | 28 8 | 18 20| 20 36| 20 47| 4 49| 18 52| 22 70| 12 84| 8
And this is the perimeter for each group.
The result is then:
sum A*P 1930
--- Part Two ---
Fortunately, the Elves are trying to order so much fence that they qualify for a bulk discount!
Under the bulk discount, instead of using the perimeter to calculate the price, you need to use the number of sides each region has. Each straight section of fence counts as a side, regardless of how long it is.
Consider this example again:
AAAA BBCD BBCC EEEC
The region containing type
A
plants has4
sides, as does each of the regions containing plants of typeB
,D
, andE
. However, the more complex region containing the plants of typeC
has8
sides!Using the new method of calculating the per-region price by multiplying the region's area by its number of sides, regions
A
throughE
have prices16
,16
,32
,4
, and12
, respectively, for a total price of80
.The second example above (full of type
X
andO
plants) would have a total price of436
.Here's a map that includes an E-shaped region full of type
E
plants:EEEEE EXXXX EEEEE EXXXX EEEEE
The E-shaped region has an area of
17
and12
sides for a price of204
. Including the two regions full of typeX
plants, this map has a total price of236
.This map has a total price of
368
:AAAAAA AAABBA AAABBA ABBAAA ABBAAA AAAAAA
It includes two regions full of type
B
plants (each with4
sides) and a single region full of typeA
plants (with4
sides on the outside and8
more sides on the inside, a total of12
sides). Be especially careful when counting the fence around regions like the one full of typeA
plants; in particular, each section of fence has an in-side and an out-side, so the fence does not connect across the middle of the region (where the twoB
regions touch diagonally). (The Elves would have used the Möbius Fencing Company instead, but their contract terms were too one-sided.)The larger example from before now has the following updated prices:
- A region of
R
plants with price12 * 10 = 120
.- A region of
I
plants with price4 * 4 = 16
.- A region of
C
plants with price14 * 22 = 308
.- A region of
F
plants with price10 * 12 = 120
.- A region of
V
plants with price13 * 10 = 130
.- A region of
J
plants with price11 * 12 = 132
.- A region of
C
plants with price1 * 4 = 4
.- A region of
E
plants with price13 * 8 = 104
.- A region of
I
plants with price14 * 16 = 224
.- A region of
M
plants with price5 * 6 = 30
.- A region of
S
plants with price3 * 6 = 18
.Adding these together produces its new total price of
1206
.What is the new total price of fencing all regions on your map?
Now we need to calculate the number of sides.
Let's look at a specific group as an example, with id 6.
" #" g=6 " ## " " ### " " ## " " ### " " # " " ## " " # " " " " " " "
We can see where we would need to add an edge on "top", by comparing each row with the previous row. Prior is useful here, as this is exactly where a value is greater than the prior value.
" #" (>) prior g=6 " ## " " # " " # " " ## " " " " # " " " " " " " " "
We then want to apply (>)prior
to each row, to find the starts of groups.
" #" (>)prior' (>) prior g=6 " # " " # " " # " " # " " " " # " " " " " " " " "
Now, we can sum the matrix by applying sum twice. All that's left is to do it for the other 4 directions, which can be done by applying the same function to all 4 rotations.
First of all, save this as a function, called tops
, calculating the number of sides we need to put "on top" of our group.
tops:{sum sum (>)prior' (>)prior x}
Now, reverse flip x
will rotate x by 90 degrees, so we apply that 3 times with Do.
3 {reverse flip x}\ 3 3#"abcdefghi" "abc" "def" "ghi" "cfi" "beh" "adg" "ihg" "fed" "cba" "gda" "heb" "ifc"
Now, for a given id, we want to find where the the groups are equal to that id, find all rotations, and use our tops function to count the number of sides.
{sum tops each 3 {reverse flip x}\ g=x} 6 22i
With the example input this is the correct number of sides, 22, for the region of C plants.
Now, to get the result, apply this for each id, and multiply by the area.
sum A*{sum tops each 3 {reverse flip x}\ g=x} each key A 1206
n: count i: read0 `12.txt
shifts:{count[x]#''(prev';next';prev;next)@\:x}
adj: i =/: shifts i
g: {x &/ (999999*not adj)+0^shifts x} over (2#n)#til n*n
A: count each group raze g
P: sum each (raze sum g <>/: shifts g) group raze g
sum A*P
tops: {sum sum (>)prior'(>)prior x}
sum A*{sum tops each 3 {reverse flip x}\ g=x} each key A