◂ 20 décembre 2022 ▸
Grove Positioning System : Déplacer des valeurs dans un tableau circulaire un grand nombre de fois
-6287
6707
6969
2869
4724
⋮
- code.py
f = open("input.txt", 'r')
lines = [line[:-1] for line in f.readlines()]
DESCRIPTION_KEY = 811589153 # pour partie 2
NB_REPETITIONS = {1:1, 2:10}
nombres = []
zero = None
identifiant = 0
for line in lines:
nombres.append((identifiant, int(line)))
if nombres[-1][1] == 0:
zero = nombres[-1]
identifiant += 1
for partie in [1,2]:
if partie == 2:
for i in range(len(nombres)):
nombres[i] = (nombres[i][0], DESCRIPTION_KEY*nombres[i][1])
nombresDeplaces = nombres[:]
for _ in range(NB_REPETITIONS[partie]):
for n in nombres:
indiceDepart = nombresDeplaces.index(n)
nombresDeplaces.pop(indiceDepart)
indiceArrivee = (indiceDepart + n[1] - 1) % len(nombresDeplaces) + 1
if indiceArrivee == len(nombresDeplaces):
nombresDeplaces = nombresDeplaces + [n]
else:
nombresDeplaces = nombresDeplaces[:indiceArrivee] + [n] + nombresDeplaces[indiceArrivee:]
indiceDuZero = nombresDeplaces.index(zero)
resultat = 0
for decalage in [1000, 2000, 3000]:
resultat += nombresDeplaces[ (indiceDuZero + decalage) % len(nombresDeplaces) ][1]
print("Réponse partie "+str(partie)+":", resultat)