advent-of-code/2024/23/one.py
2024-12-25 12:59:49 +01:00

37 lines
842 B
Python

#!/usr/bin/env python3
import collections
import sys
input_file = sys.argv[1]
with open(input_file) as fd:
lines = [line.rstrip() for line in fd.readlines()]
connections = [tuple(line.split("-")) for line in lines]
codi: collections.defaultdict[str, set[str]] = collections.defaultdict(set)
for connection in connections:
a, b = connection
codi[a].add(b)
codi[b].add(a)
threes: set[tuple[str, ...]] = set()
for connection in connections:
a, b = connection
ac, bc = codi[a], codi[b]
iis = ac.intersection(bc)
for i in iis:
threel = [a, b, i]
if not (a.startswith("t") or b.startswith("t") or i.startswith("t")):
continue
threel.sort()
threes.add(tuple(threel))
for three in threes:
print(three)
print(len(threes))
# 11011 too high (forgot starts with t)