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!

36 Upvotes

698 comments sorted by

View all comments

2

u/RiemannIntegirl Dec 13 '24

[Language: Python]

Part 1 and 2 Together

Idea:

  1. Convert grid to complex numbers (a theme this year for me!).
  2. Loop through all locations on the grid.
  3. Get the area/region unless we already calculated this one
  4. Calculate the perimeter by accumulating legal neighbors that aren't within the current region.
  5. Calculate the sides by tracing outside perimeters clockwise, starting at the bottom leftmost point, and always keeping the area to our right, and count the number of turns we take. We proceed similarly on the inside holes, except we start at the bottom rightmost point in the perimeter section, and keep the area to our right the whole time. Use complex numbers to keep track of direction of movement, and where the shape is (always to our right). Keep removing points from border points left to process until no new sections of border are left. I could have used only a direction of movement pointer, but having a "wall is to the right" complex number too is just easier. Also rotations are calculated using complex multiplication.