-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25_Snowverload.py
85 lines (64 loc) · 1.82 KB
/
25_Snowverload.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
import heapq
import sys, collections, re, itertools, functools, math
from lib import *
inp = get_input()
G = collections.defaultdict(set)
for l in inp:
a,b = l.split(": ")
b = b.split()
G[a].update(b)
for c in b:
G[c].add(a)
C = collections.defaultdict(int)
for i, s in enumerate(G.keys()):
dist = {n: float("inf") for n in G.keys()}
prev = {n: None for n in G.keys()}
dist[s] = 0
Q = [(0,s)]
V = set()
while Q:
d, n = heapq.heappop(Q)
if n in V:
continue
V.add(n)
for sn in G[n]:
nd = dist[n] + 1
if nd < dist[sn]:
dist[sn] = nd
prev[sn] = n
heapq.heappush(Q, (nd, sn))
for n in G.keys():
g = n
while g:
C[g] +=1
g = prev[g]
# print(s, i, len(G.keys()), i/len(G.keys()))
s = list(zip(*sorted(C.items(), key=lambda x: x[1], reverse=True)[:12]))[0]
def get_combinations_dict(lst):
pairs = itertools.combinations(lst, 2)
group_combs = itertools.combinations(pairs, 3)
combs = []
for g in group_combs:
gt = [e for t in g for e in t]
if len(set(gt)) == 6:
combs.append(dict(g))
return combs
for S in [l for f in itertools.combinations(s, r=6) for l in get_combinations_dict(f)]:
def dfs(s):
Q = collections.deque([s])
V = set()
while Q:
n = Q.popleft()
if n in V: continue
V.add(n)
for sn in G[n]:
if n in S and sn == S[n]: continue
if sn in S and n == S[sn]: continue
Q.append(sn)
return len(V)
a = dfs(list(S.keys())[0])
b = dfs(list(S.values())[0])
if a != b != len(G.keys()):
print(a*b)
break
print("-") # there is no part 2