Day 23 - Advent of Code 2024
23 December 2024
Working solutions for the day 23 puzzles.
Part One
""" day_23_01.py """
# usage: python3 day_23_01.py < input
import sys
from itertools import combinations
def starts_with_t(items):
""" an item starts with t """
return any(i.startswith('t') for i in items)
network = {}
with sys.stdin as infile:
for link in infile:
pc1, pc2 = link.strip().split('-')
network[pc1] = network.setdefault(pc1, []) + [pc2]
network[pc2] = network.setdefault(pc2, []) + [pc1]
interconnected = set()
for host, pcs in network.items():
for pc1, pc2 in combinations(pcs, 2):
if pc1 in network[pc2]:
interconnected.add(tuple(sorted([host, pc1, pc2])))
print(len(list(filter(starts_with_t, interconnected))))
Part Two
""" day_23_02.py """
# usage: python3 day_23_02.py < input
import sys
from itertools import combinations
network = {}
with sys.stdin as infile:
for link in infile:
pc1, pc2 = link.strip().split('-')
network[pc1] = network.setdefault(pc1, []) + [pc2]
network[pc2] = network.setdefault(pc2, []) + [pc1]
password = []
pcs = list(network.keys())
while pcs:
pc = pcs.pop()
connected = [pc] + network[pc]
for pc1, pc2 in combinations(connected, 2):
if pc1 in connected and pc2 not in network[pc1]:
connected.remove(pc1)
if len(connected) > len(password):
password = connected
print(','.join(sorted(password)))