-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09.a.py
More file actions
39 lines (33 loc) · 996 Bytes
/
09.a.py
File metadata and controls
39 lines (33 loc) · 996 Bytes
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
with open("2015/09.input.txt", encoding="utf-8") as file:
data = file.read()
lines = data.splitlines()
lines = filter(lambda x: len(x) > 0, lines)
distances: dict[(int, int), int] = {}
cities: list[str] = []
for line in lines:
parts = line.split()
city1 = parts[0]
if city1 not in cities:
cities.append(city1)
city2 = parts[2]
if city2 not in cities:
cities.append(city2)
distance = int(parts[4])
distances[(cities.index(city1), cities.index(city2))] = distance
def check(current: list[int]):
if len(current) == len(cities):
return sum(
distances.get(x) or distances.get((x[1], x[0]))
for x in zip(current, current[1:])
)
return min(
check(current + [i])
for i in range(len(cities))
if i not in current
and (
len(current) == 0
or (current[-1], i) in distances
or (i, current[-1]) in distances
)
)
print(check([]))