◂ 2 décembre 2024 ▸
Red-Nosed Reports : Vérification de monotonie stricte, avec tolérence d'une erreur max.
2 4 6 9 10 9
45 48 51 52 55 58 60 60
26 26 25 22 19 16 14
⋮
- code.py
import re
f = open("input.txt", 'r', encoding='utf-8')
lines = [line[:-1] for line in f.readlines()]
nb1 = nb2 = 0
MAX_CHANGE = 3
def is_slow_strict_monotonic(values):
differences = [values[i] - values[i-1] for i in range(1,len(values))]
positive = (differences[0] > 0)
return all( ((d>0) == positive) and (0 < abs(d) <= MAX_CHANGE) for d in differences )
for line in lines:
values = [ int(token) for token in line.split() ]
if is_slow_strict_monotonic(values):
nb1 += 1
nb2 += 1
else:
for i in range(len(values)):
new_values = values[:]
new_values.pop(i)
# new_values = values[:i] + values[i+1:] # 2 times slower for big lists
if is_slow_strict_monotonic(new_values):
nb2 += 1
break
print("Réponse partie 1:", nb1)
print("Réponse partie 2:", nb2)