-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.py
91 lines (79 loc) · 1.79 KB
/
23.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
from collections import defaultdict
example = """kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn"""
def p1():
network = defaultdict(list)
for line in open("input/23.txt").read().strip().split("\n"):
p1 = line.split("-")[0]
p2 = line.split("-")[1]
network[p1].append(p2)
network[p2].append(p1)
groups = set()
for k, con in network.items():
for i in range(len(con) - 1):
for j in range(i + 1, len(con)):
group = tuple(sorted((k, con[i], con[j])))
contains_t = False
for g in group:
if g[0] == "t":
contains_t = True
break
if con[i] in network[con[j]] and contains_t:
groups.add(group)
print("p1:", len(groups))
def p2():
network = defaultdict(list)
for line in open("input/23.txt").read().strip().split("\n"):
p1 = line.split("-")[0]
p2 = line.split("-")[1]
network[p1].append(p2)
network[p2].append(p1)
largest_lan = []
for key, con in network.items():
for i in range(len(con) - 1):
lan = []
lan.append(key)
lan.append(con[i])
for j in range(i + 1, len(con)):
included = True
for k in lan:
if con[j] not in network[k]:
included = False
break
if included:
lan.append(con[j])
if len(lan) > len(largest_lan):
largest_lan = lan
print("p2:", ",".join(sorted(largest_lan)))
p1()
p2()