Move scripts dir inside hm

And remove weird path contraptions
This commit is contained in:
Geoffrey Frogeye 2023-11-30 22:09:44 +01:00
parent 050901da2f
commit edeef96133
Signed by: geoffrey
GPG key ID: C72403E7F82E6AD8
49 changed files with 2 additions and 11 deletions

76
hm/scripts/ter Executable file
View file

@ -0,0 +1,76 @@
#!/usr/bin/env nix-shell
#! nix-shell -i python3 --pure
#! nix-shell -p python3
import sys
from math import inf
gares = sys.argv[1:]
N = len(gares)
if N < 2:
print("Ben reste chez toi alors.")
sys.exit(1)
def trajet_str(a, b):
return f"{gares[a]} → {gares[b]}"
def chemin_str(stack):
return ", ".join(
[trajet_str(stack[i], stack[i + 1]) for i in range(len(stack) - 1)]
)
# Demande des prix des trajets
prices = dict()
for i in range(N):
for j in range(N - 1, i, -1):
p = None
while not isinstance(p, float):
try:
p = float(
input(f"Prix du trajet {trajet_str(i, j)} ? ").replace(",", ".")
)
except ValueError:
print("C'est pas un prix ça !")
if i not in prices:
prices[i] = dict()
prices[i][j] = float(p)
# Calcul des prix des chemins
miniPrice = +inf
miniStack = None
maxiPrice = -inf
maxiStack = None
def register_path(stack):
price = sum([prices[stack[i]][stack[i + 1]] for i in range(len(stack) - 1)])
global miniPrice, maxiPrice, miniStack, maxiStack
if price < miniPrice:
miniPrice = price
miniStack = stack.copy()
if price > maxiPrice:
maxiPrice = price
maxiStack = stack.copy()
print(f"{chemin_str(stack)} = {price:.2f} €")
stack = [0]
while stack[0] == 0:
if stack[-1] >= N - 1:
register_path(stack)
stack.pop()
stack[-1] += 1
else:
stack.append(stack[-1] + 1)
print(f"Prix minimum: {chemin_str(miniStack)} = {miniPrice:.2f} €")
print(f"Prix maximum: {chemin_str(maxiStack)} = {maxiPrice:.2f} €")