Skip to content

Commit

Permalink
Day 23, 2024 - Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
xunoaib committed Dec 23, 2024
1 parent 12833d4 commit 46f7cec
Showing 1 changed file with 17 additions and 48 deletions.
65 changes: 17 additions & 48 deletions 2024/day23/day23.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,35 @@

import sys
from collections import defaultdict
from itertools import combinations


def find_connections(c, seq):
for n in g[c]:
if n > c:
newseq = seq + (n,)
if set(newseq).issubset(g[n]):
results.add(newseq)
find_connections(n, newseq)

lines = sys.stdin.read().strip().split('\n')

g = defaultdict(set)

for line in lines:
a,b = line.split('-')
g[a].add(b)
g[b].add(a)

def part1():

ans = 0
for p in combinations(g, r=3):
if not any(c.startswith('t') for c in p):
continue

a,b,c = p
sa = {a} | g[a]
sb = {b} | g[b]
sc = {c} | g[c]

if set(p).issubset(sa & sb & sc):
ans += 1
return ans
g[a] |= {a, b}
g[b] |= {a, b}

results = set()

def all_shared(nodes):
return all(set(nodes).issubset(g[n]) for n in nodes)

def find_pools(c, seq=tuple()):
if not seq:
seq = (c,)

for n in g[c]:
if n <= c: # avoid exploring the same route twice
continue
newseq = seq + (n,)
if all_shared(newseq):
results.add(tuple(sorted(newseq)))
find_pools(n, newseq)

def part2():
for c in g:
g[c].add(c)

for c in g:
find_pools(c)

ans = sorted(max(results, key=len))
return ','.join(ans)

for c in g:
find_connections(c, (c,))

# a1 = part1()
# print('part1:', a1)
a1 = sum(len(p) == 3 and any(n.startswith('t') for n in p) for p in results)
print('part1:', a1)

a2 = part2()
a2 = ','.join(max(results, key=len))
print('part2:', a2)

# assert a1 == 1062
assert a1 == 1062
assert a2 == 'bz,cs,fx,ms,oz,po,sy,uh,uv,vw,xu,zj,zm'

0 comments on commit 46f7cec

Please sign in to comment.