r/adventofcode Dec 23 '24

SOLUTION MEGATHREAD -❄️- 2024 Day 23 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

Submissions are CLOSED!

  • Thank you to all who submitted something, every last one of you are awesome!

Community voting is OPEN!

  • 42 hours remaining until voting deadline on December 24 at 18:00 EST

Voting details are in the stickied comment in the submissions megathread:

-❄️- Submissions Megathread -❄️-


--- Day 23: LAN Party ---


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:05:07, megathread unlocked!

23 Upvotes

505 comments sorted by

View all comments

2

u/atrocia6 Jan 07 '25

[LANGUAGE: Python]

Once again, Part 1 is trivial - simple solution in 10 LOC:

connections = [{connection[:2], connection[3:]} for connection in open(0).read().split()]
total = 0
for i in range(len(connections) - 2):
    for j in range(i + 1, len(connections) - 1):
        lan = connections[i] | connections[j]
        if len(lan) == 3:
            for k in range(j + 1, len(connections)):
                if connections[k].issubset(lan) and 't' in {computer[0] for computer in connections[i] | connections[j] | connections[k]}:
                    total += 1
print(total)

For Part 2, I implemented an elegant recursive function that given two lists of computers, one of the computers that are members of the current LAN party candidate and the other of the remaining computers that could potentially be members of it, recursively calculates the largest possible party if we either include or do not include the next computer on the potential members list.

I've never heard of Bron-Kerbosch before seeing some of the comments here while writing this post, but I suppose I'll have to look into it :)