Today’s input is the layout of the huge garden plot you are at.
An example input looks like this:
It’s a 2D-grid again where each letter signals a type of crop that grows in that location.
Parsing
If you’ve been following along, you know what I do when I see a grid puzzle by now, Point!
Point
I stored each letter in a map again.
This is not the most efficient way to store this, but it’s very nice to work with and think about.
Part 1
The elves want to put up fences around regions of plots that grow the same crop.
A plot that touches another plot (up/right/down/left) that grows the same crop belongs to the same region.
You need to figure out each plot’s area and perimeter.
The price for an amount of fence is area * perimeter.
Don’t ask why, the answer is ✨reasons✨.
The question asks for the sum of all region prices.
Some skeleton/pseudo-code
Helpers
Time to write the code that can make my skeleton-code a reality.
Code
A global set makes sure I don’t count garden plots that already contribute to the sum twice.
Each time a shape (a set of Points) is calculated,
those Points are added to the set so they can’t be included in future shapes.
You might have noticed the distinct lack of area(),
well, that’s a oneliner and making a function for it would be a bit silly.
Part 2
Instead of the circumference of garden plots, use the amount of sides the plot has.
The question asks for the sum of all region prices again.
Some skeleton/pseudo-code again, changing circumference to sides:
Helpers
Updating the helpers from part 1.
The most interesting is the sides function, which I adapted from solutions I saw only.
It chooses a direction, goes in it until the garden plot ends, and then follows the side in the perpendicular direction until that ends.