#!/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)