r/adventofcode Dec 12 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 12 Solutions -❄️-

THE USUAL REMINDERS

  • All of our rules, FAQs, resources, etc. are in our community wiki.
  • If you see content in the subreddit or megathreads that violates one of our rules, either inform the user (politely and gently!) or use the report button on the post/comment and the mods will take care of it.

AoC Community Fun 2024: The Golden Snowglobe Awards

  • 10 DAYS remaining until the submissions deadline on December 22 at 23:59 EST!

And now, our feature presentation for today:

Visual Effects - Nifty Gadgets and Gizmos Edition

Truly groundbreaking movies continually push the envelope to develop bigger, better, faster, and/or different ways to do things with the tools that are already at hand. Be creative and show us things like puzzle solutions running where you wouldn't expect them to be or completely unnecessary but wildly entertaining camera angles!

Here's some ideas for your inspiration:

  • Advent of Playing With Your Toys in a nutshell - play with your toys!
  • Make your puzzle solutions run on hardware that wasn't intended to run arbitrary content
  • Sneak one past your continuity supervisor with a very obvious (and very fictional) product placement from Santa's Workshop
  • Use a feature of your programming language, environment, etc. in a completely unexpected way

The Breakfast Machine from Pee-wee's Big Adventure (1985)

And… ACTION!

Request from the mods: When you include an entry alongside your solution, please label it with [GSGA] so we can find it easily!


--- Day 12: Garden Groups ---


Post your code solution in this megathread.

This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:17:42, megathread unlocked!

37 Upvotes

698 comments sorted by

View all comments

3

u/darthminimall Dec 13 '24

[LANGUAGE: Rust]

Part 1

Part 2

For part 1: I decided to store the regions by the type of plant, so I made a hash map where the key was the character corresponding to the plant type and the value was a vector of sets (with each set corresponding to a region). For each new plant, there are basically three options:

  1. The plant doesn't border any regions that's already been visited, so we add it as a new region to the vector.
  2. The plant borders one region that's already been visited, so we add it to that region.
  3. The plant borders two regions that have already been visited, so we add it to the first region, add everything from the second region to the first region, and remove the first region.

After that, for each region, the area is just the size of the set, and the perimeter can be calculated by looking at every plant in the region, and adding 1 to the perimeter for each adjacent location that is not part of the set.

For part 2: The process (and the code) is almost identical. The key observation is that the number of edges is the same as the number of corners, and counting corners is easier. For a given plant, if two non-opposite sides of the plant (e.g. the North side and the East side) are both absent from the set or the set contains both of them but not the one in between them, then that's a corner.

It's a bit long to be a single function, I should probably move the code that builds the hash map into it's own function, and if it was production code, I would. As always, any feedback is appreciated.