Spaces:
Running
Running
| from collections import defaultdict | |
| from itertools import combinations | |
| def parse_input(file): | |
| connections = defaultdict(set) | |
| with open(file, 'r') as f: | |
| for line in f: | |
| a, b = line.strip().split('-') | |
| connections[a].add(b) | |
| connections[b].add(a) | |
| return connections | |
| def find_triplets(connections): | |
| computers = list(connections.keys()) | |
| triplets = [] | |
| for a, b, c in combinations(computers, 3): | |
| if (b in connections[a] and | |
| c in connections[a] and | |
| c in connections[b]): | |
| triplets.append((a, b, c)) | |
| return triplets | |
| def count_t_triplets(triplets): | |
| count = 0 | |
| for triplet in triplets: | |
| if any(comp.startswith('t') for comp in triplet): | |
| count += 1 | |
| return count | |
| def find_largest_clique(connections): | |
| computers = list(connections.keys()) | |
| n = len(computers) | |
| max_clique = [] | |
| # Try all possible combinations, starting from largest | |
| for size in range(n, 0, -1): | |
| for combo in combinations(computers, size): | |
| # Check if all computers in combo are connected to each other | |
| is_clique = all( | |
| all(b in connections[a] for b in combo if b != a) | |
| for a in combo | |
| ) | |
| if is_clique: | |
| return sorted(combo) | |
| return max_clique | |
| # Part 1 | |
| connections = parse_input("input.txt") | |
| triplets = find_triplets(connections) | |
| result1 = str(count_t_triplets(triplets)) | |
| print(result1) | |
| # Part 2 | |
| largest_clique = find_largest_clique(connections) | |
| result2 = ','.join(largest_clique) | |
| print(result2) |