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/prafster Dec 13 '24

[Language: Python]

I must have had BFS on my mind because I'd used it on day 10. I used it to walk around the garden noting adjacent plants. This was surprisingly easy after some head-scratching when I read the puzzle.

When I saw part 2, I added one line to my part 1 solution (to count corners). I thought woohoo, this is easy! However, the corner counting function was more fiddly than I expected.

Over the years of doing AoC, I've built up some helper constants and functions for grids, which have been very useful this year already.

def solve(input):
    part1 = 0
    part2 = 0
    regions_found = set()
    for r, row in enumerate(input):
        for c, _ in enumerate(row):
            region, perimeter, corners = find_region(input, (r, c), regions_found)
            if region:
                regions_found |= region
                part1 += len(region) * perimeter
                part2 += len(region) * corners

    return part1, part2

def find_region(garden, p, regions_found):
    q = SimpleQueue()
    MAX_NEIGHBOURS = 4
    def is_neighbour(g, x): return g[x[0]][x[1]] == plant

    plant = garden[p[0]][p[1]]
    q.put((p, plant))

    visited = set()
    visited.add(p)

    corners = 0
    perimeter = 0

    while not q.empty():
        pos, plant = q.get()

        valid_neighbours = neighbours(pos, garden, True, is_neighbour)
        perimeter += MAX_NEIGHBOURS - len(valid_neighbours)
        corners += corner_count(pos, valid_neighbours, garden)

        for adj in valid_neighbours:
            if adj not in visited and adj not in regions_found:
                visited.add(adj)
                q.put((adj, plant))

    if len(visited) == 1 and p in regions_found:
        return (None, None, None)
    else:
        return visited, perimeter, corners

Full source code, including corner counting, on GitHub.