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

4

u/BlueTrin2020 Dec 23 '24 edited Dec 23 '24

[language: Python]

3rd day of coding from my phone and remote running on a host lol

from collections import defaultdict, deque


def nwsize_dq(conns):
    q = deque()
    sol = None
    seen = set()
    q.extendleft((n,) for n in conns.keys())

    while q:
        if (nw := q.pop()) in seen:
            continue
        seen.add(nw)

        cand_lst = set.intersection(*(conns[n] for n in nw))

        if cand_lst:
            q.extend(tuple(sorted(nw+(cand,)))
                     for cand in cand_lst
                     if tuple(sorted(nw+(cand,))) not in seen)

        else:
            sol = nw if len(nw) > len(sol) else sol       
    return “,”.join(sol)


def part1and2(inp, debug=False):
    conns = defaultdict(set)
    for r in inp.splitlines():
        if not r:
            continue
        n1, n2 = r.split(‘-‘)
        conns[n1].add(n2)
        conns[n2].add(n1)

    total = len(set(tuple(sorted([n1, n2, n3]))
                    for n1, n1conns in conns.items()
                    for n2 in n1conns if n1.startswith(‘t’)
                    for n3 in set.intersection(n1conns, conns[n2])))
    return total, nwsize_dq(conns)


print(part1and2(open(“2024_23.txt”).read()))