-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday23.py
30 lines (22 loc) · 865 Bytes
/
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
import time, itertools as itt, collections as coll
def load(file):
friends = coll.defaultdict(set)
with open(file) as f:
for row in f.read().split('\n'):
a, b = row.split('-')
friends[a].update((a, b))
friends[b].update((a, b))
return friends
def solve(p):
part1 = len({comb for conn in p.values()
for comb in itt.combinations(sorted(conn), 3)
if all(set(comb).issubset(p[c]) for c in comb) and
any(t[0] == 't' for t in comb)})
networks = [{comp} for comp in p]
for network, comp in itt.product(networks, p):
if network.issubset(p[comp]): network.add(comp)
part2 = ','.join(sorted(max(networks, key=len)))
return part1, part2
time_start = time.perf_counter()
print(f'Solution: {solve(load("day23.txt"))}')
print(f'solved in {time.perf_counter() - time_start:.4f} sec.')