-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23_2_bf.py
37 lines (28 loc) · 915 Bytes
/
day23_2_bf.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
from collections import defaultdict
import time
start_time = time.time()
is_test = False
day = 23
input_file = f'./input/day{day}{"_test" if is_test else ""}.txt'
def sol():
lines = open(input_file).read().splitlines()
points = set()
connects = defaultdict(set)
for line in lines:
a, b = line.strip().split('-')
points.add(a)
points.add(b)
connects[a].add(b)
connects[b].add(a)
cliques = set()
ps = sorted(list(points))
def backtrack(group, candidates, start):
cliques.add(frozenset(group))
for i in range(start, len(candidates)):
p = candidates[i]
if all((p in connects[v]) for v in group):
backtrack(group | {p}, candidates, i + 1)
backtrack(set(), ps, 0)
print(max((len(r), ','.join(sorted(r))) for r in cliques))
sol()
print(f'{(time.time() - start_time) * 1000:.3f}ms')