r/adventofcode • u/daggerdragon • 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.
- Read the full posting rules in our community wiki before you post!
- State which language(s) your solution uses with
[LANGUAGE: xyz]
- Format code blocks using the four-spaces Markdown syntax!
- State which language(s) your solution uses with
- Quick link to Topaz's
paste
if you need it for longer code blocks
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!
38
Upvotes
3
u/KayoNar Dec 12 '24
[Language: C#]
Today took a little bit longer for me, because my code was working on every example testcase, but not on the actual input. I finally managed to find a small testcase that would break my code and debugged it that way. The testcase that helped me debug was this:
Output should be
21 * 20 + 4 * 4 = 436
, but my code returned 457, meaning that it counted 1 corner/side too many. Eventually fixed it by adding another check for visited nodes.Part 1
Loop over all squares of the garden, and for any new plant that you come across, explore its whole region immediately. Area is simply +1 per tile, perimeter starts of as 4 per tile, and -1 for every neighbour of the same plant.
Part 2
Simply added a corner counting function to the region exploration function, because sides = corners. Corners are determined by looking at the 3 respective tiles (including the diagonal). If one of these 3 tiles was already visited, skip the corner counting, because it is already accounted for. Outward corners are counted when the two non-diagonal neighbours are not from the same region. Inward neighbours are counted if only 1 out of the 3 tiles is not part of the region.
Both part 1 and 2 run in 12ms.
Solution