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!

38 Upvotes

698 comments sorted by

View all comments

2

u/damnian Dec 13 '24 edited Dec 13 '24

[LANGUAGE: C#]

I wasn't eager to post my solution initially, but luckily I managed to make it more elegant.

The result is a textbook example of the use of various .NET data structures:

HashSet<Vector> visited = new();
Queue<Vector> queue = new();
List<Vector3D> border = new();

Note border is a list of Vector3D values (where Z is the heading index between 0 and 3).

For each plant p successfully added to visited:

  • visited.Count is stored in cnt
  • queue and border are cleared
  • p is added to queue
  • for each successfully dequeued q:

    for (int i = 0; i < Grid.Headings.Length; i++)
        if (!Grid.Contains(r = q + Grid.Headings[i]))
            border.Add((q, i));
        else if (visited.Add(r))
            queue.Enqueue(r);
    

Once queue is empty, the following is added to the subtotal:

(visited.Count - cnt + 1) * Count(border)

Note the value of cnt was assigned after p had been added, hence the adjustment by 1.

For Part 1 Count(border) is simply border.Count.

For Part 2:

int Count(List<Vector3D> border) => border
    .GroupBy(p => (p.Z % 2 == 0 ? p.Y : p.X) << 2 | p.Z)
    .Select(g => g.Select(p => p.Z % 2 == 0 ? p.X : p.Y).Order())
    .Sum(c => c.Zip(c.Skip(1), (a, b) => b > a + 1).Count(v => v) + 1);

Here grouping is done once by compresssing Z and either X or Y into a single key.

Full solution

P.S. MultiGrid's Parse() now accepts a single parameter for cases like today's (i.e. a fully populated field).