-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.py
50 lines (36 loc) · 938 Bytes
/
24.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
from lib import *
input = read_input(2017, 24)
lines = input.splitlines()
comps = sorted([tuple(sorted(map(int, line.split("/")))) for line in lines])
used = set()
def solve(port):
best = 0
for i, (p, q) in enumerate(comps):
if i in used:
continue
if q == port:
p, q = q, p
if p != port:
continue
used.add(i)
best = max(best, p + q + solve(q))
used.remove(i)
return best
print(solve(0))
comps = sorted([tuple(sorted(map(int, line.split("/")))) for line in lines])
used = set()
def solve(port):
best = 0, 0
for i, (p, q) in enumerate(comps):
if i in used:
continue
if q == port:
p, q = q, p
if p != port:
continue
used.add(i)
a, b = solve(q)
best = max(best, (a + 1, p + q + b))
used.remove(i)
return best
print(solve(0)[1])