AoC day 23 (LAN Party)
https://adventofcode.com/2024/day/23unknown
python
10 months ago
2.6 kB
85
No Index
C=[c.split('-') for c in puzzle.splitlines()] # parse the pairs of connected gamers
G=defaultdict(lambda:[]) # init dictionary of gamers
for g1,g2 in C: G[g1]+=[g2]; G[g2]+=[g1] # update list of connected partners for each gamer
def part1(): # find the trios of fully interconnected gamers
trio=set() # init the set of trios
for g1,gl in G.items(): # for each gamer's list of connected partners
for g2,g3 in itertools.combinations(gl,2): # keep from all combinations of partner's pairs
if g2 in G[g3] and (g1[0]=='t' or g2[0]=='t' or g3[0]=='t'): # the connected pairs making trio of at least one 't' gamer
trio.add(tuple(sorted([g1,g2,g3]))) # sort and record this new trio
print(len(trio)) # display the nbr of unique trios
def part2(): # find the largest LAN of fully interconnected gamers
LAN=set((g,) for g in G.keys()) # start from all groups of 1 gamer
while(len(LAN)>1): # keep groups that can grow until there remains only one
nLAN=set() # init the new set of groups of incremented size
for gg in LAN: # for each gamers group of current LAN
gg=list(gg) # tuple>>list transformation only for faster search :)
for ng in (g for g in G if g not in gg): # invite a new gamer from outside the group
xt=all(ng in G[g] for g in gg) # check if he/she is connected with all current members
if xt: nLAN.add(tuple(sorted(gg+[ng]))) # if so, extend and record this new larger group
LAN=nLAN # replace previous LAN with new LAN of larger groups
print(','.join(list(LAN)[0])) # display the sorted list of members of largest LANEditor is loading...
Leave a Comment