Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- from sys import argv
- def parse(src):
- for line in src.splitlines():
- if line == '':
- continue
- first, second = line.split('-', 1)
- yield first.strip(), second.strip()
- def make_graph(connections):
- graph = defaultdict(set)
- for first, second in connections:
- graph[first].add(second)
- graph[second].add(first)
- return graph
- def find_triples(graph):
- triples = set()
- nodes = list(graph.keys())
- for index, a in enumerate(graph):
- for b in graph[a]:
- for c in nodes[index+1:]:
- if {a, b} <= graph[c]:
- triples.add(frozenset((a, b, c)))
- return triples
- def main(connections):
- total = 0
- graph = make_graph(connections)
- triples = find_triples(graph)
- for triple in triples:
- if any(x[0] == 't' for x in triple):
- total += 1
- return total
- if __name__ == '__main__':
- print(main(parse(open(argv[1]).read())))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement