File size: 2,084 Bytes
a4da721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
file = "input.txt"

def parse_input(file):
    connections = {}
    with open(file, 'r') as f:
        for line in f:
            a, b = line.strip().split('-')
            if a not in connections:
                connections[a] = set()
            if b not in connections:
                connections[b] = set()
            connections[a].add(b)
            connections[b].add(a)
    return connections

def find_triangles(connections):
    triangles = set()
    for a in connections:
        for b in connections[a]:
            if b > a:  # Ensure unique pairs
                for c in connections[b]:
                    if c > b and c in connections[a]:
                        triangles.add(tuple(sorted([a, b, c])))
    return triangles

def filter_triangles(triangles):
    return [triangle for triangle in triangles if any(comp.startswith('t') for comp in triangle)]

def find_largest_clique(connections):
    def is_clique(potential_clique):
        return all(connections[a].issuperset(potential_clique - {a}) for a in potential_clique)

    def expand_clique(current_clique, candidates):
        max_clique = current_clique
        for candidate in candidates:
            new_clique = current_clique | {candidate}
            new_candidates = candidates & connections[candidate]
            if is_clique(new_clique):
                expanded_clique = expand_clique(new_clique, new_candidates)
                if len(expanded_clique) > len(max_clique):
                    max_clique = expanded_clique
        return max_clique

    all_computers = set(connections.keys())
    largest_clique = set()
    for computer in all_computers:
        clique = expand_clique({computer}, connections[computer])
        if len(clique) > len(largest_clique):
            largest_clique = clique
    return largest_clique

connections = parse_input(file)
triangles = find_triangles(connections)
filtered_triangles = filter_triangles(triangles)
print(len(filtered_triangles))

largest_clique = find_largest_clique(connections)
password = ','.join(sorted(largest_clique))
print(password)