Add: 07->08

This commit is contained in:
PedroEdiaz
2025-10-31 15:56:51 -06:00
parent e0e6bfbc97
commit 0b4bd32e3e
3 changed files with 217 additions and 0 deletions

58
2015/09.py Normal file
View File

@@ -0,0 +1,58 @@
import re
# Fill Input
try:
with open("09.txt", 'r', encoding='utf-8') as file:
input = file.read()
except Exception as e:
print(f"An error occurred: {e}")
# Result
def perm(X, visited = []):
if len(visited) == len(X):
return [visited]
res = []
for a in X:
if a not in visited:
for i in perm(X, [*visited, a]):
res.append(i)
return res;
path = {}
for line in input.split('\n'):
if not (re_match := re.search(r"(\w+) to (\w+) = (\d+)", line)):
break
f = re_match.group(1)
t = re_match.group(2)
d = int(re_match.group(3))
if f in path:
path[f][t] = d
else:
path[f] = {t:d}
if t in path:
path[t][f] = d
else:
path[t] = {f:d}
res = []
for p in perm(list(path)):
sum = 0;
last = None
for c in p:
if not last:
last = c
continue
sum += path[last][c]
last = c
res.append(sum)
print(min(res), max(res))