-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.py
102 lines (72 loc) · 1.88 KB
/
day23.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sys
import networkx as nx
import matplotlib.pyplot as plt
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
connections = {}
with open(inputfile,"r") as input:
for line in input:
pcs = line.rstrip().split("-")
if pcs[0] in connections:
connections[pcs[0]].add(pcs[1])
else:
connections[pcs[0]] = {pcs[1]}
if pcs[1] in connections:
connections[pcs[1]].add(pcs[0])
else:
connections[pcs[1]] = {pcs[0]}
def find_triplets():
global connections
triplets = set()
for one in connections.keys():
for two in connections[one]:
commons = connections[two].intersection(connections[one])
for c in commons:
triplets.add(tuple(sorted([one,two,c])))
return triplets
def find_options():
global connections
options = set()
for one in connections.keys():
for two in connections[one]:
commons = connections[two].intersection(connections[one])
if len(commons) > 1:
options.add(one)
options.add(two)
options.update(commons)
return options
def find_t(triplets):
result = []
for three in triplets:
if three[0][0] == "t" or three[1][0] == "t" or three[2][0] == "t":
result.append(three)
return result
def run_part1():
print(connections)
triplets = find_triplets()
result1 = find_t(triplets)
print(result1)
print(len(result1))
def run_part2():
global connections
options = find_options()
G = nx.Graph()
G.add_nodes_from(options)
for o in options:
for edge in connections[o]:
G.add_edge(o,edge)
print(G)
pos=nx.spring_layout(G)
nx.draw(G, pos)
nx.draw_networkx_labels(G, pos)
# plt.show()
longest = []
for c in nx.find_cliques(G):
if len(c) > len(longest):
longest = c
print(",".join(sorted(longest)))
run_part1()
run_part2()