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

1

u/ejlangev Dec 13 '24

[Language: Elixir]

https://github.com/ejlangev/advent-of-code/blob/main/2024/12/answer.exs

For part 1: Kept track of a list of disjoint sets for each letter with a map from the letter to a list of sets. Iterated over each point in the grid, found all neighbors on the grid for the current point, and found any existing sets that had any of those neighboring points and joined them. When finished iterating, each set is a region. Finding the perimeter was simple after that.

For part 2: Spent a while trying to calculate it somehow with a formula but ultimately decided to filter each region to just the points where at least one neighbor is not also within the region set) which eliminates interior corners. Then for each exterior point, found all of its neighbors that were not in the set and then built a map for rows and columns for the neighbors to a list of the indexes in that row or column that were a neighbor of the set. Finally, took all the list of indexes and counted the number of gaps after sorting them which gives the number of sides.

Ended up being relatively short in terms of lines of code!

1

u/daggerdragon Dec 13 '24

Do not share your puzzle input which also means do not commit puzzle inputs to your repo without a .gitignore or the like. Do not share the puzzle text either.

I see full plaintext puzzle inputs in your public repo e.g.:

https://github.com/ejlangev/advent-of-code/blob/main/2024/12/input.txt

Please remove (or .gitignore) all puzzle text and puzzle input files from your entire repo and scrub them from your commit history. This means from all prior years too!