trois premiers jours
This commit is contained in:
62
day2.py
Normal file
62
day2.py
Normal file
@ -0,0 +1,62 @@
|
||||
import numpy as np
|
||||
|
||||
with open('day2.txt') as f:
|
||||
l = [line.rstrip().split() for line in f]
|
||||
|
||||
# l = [[7, 6, 4, 2, 1],
|
||||
# [1, 2, 7, 8, 9],
|
||||
# [9, 7, 6, 2, 1],
|
||||
# [1, 3, 2, 4, 5],
|
||||
# [8, 6, 4, 4, 1],
|
||||
# [1, 3, 6, 7, 9]]
|
||||
|
||||
print(l)
|
||||
|
||||
# l = np.loadtxt("day2.txt")
|
||||
|
||||
def test_if_safe(diff,inc):
|
||||
# inc = np.sign(diff)
|
||||
if abs(diff) > 3 or abs(diff) == 0:
|
||||
print("Trop grand")
|
||||
return False
|
||||
elif np.sign(inc) != np.sign(diff):
|
||||
print("Changement de sens")
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def loop(list):
|
||||
for k in range(len(list)-1):
|
||||
diff2 = int(list[k+1])-int(list[k])
|
||||
if k == 0:
|
||||
inc2 = np.sign(diff2)
|
||||
|
||||
print("diff = ", diff2)
|
||||
|
||||
|
||||
safe2 = test_if_safe(diff2,inc2)
|
||||
if not safe2 :
|
||||
break
|
||||
inc2 = np.sign(diff2)
|
||||
print("safe in loop", safe2)
|
||||
return safe2
|
||||
|
||||
|
||||
|
||||
cnt = 0
|
||||
|
||||
for report in l:
|
||||
incs = []
|
||||
inc = 0
|
||||
|
||||
safe = True
|
||||
print(report)
|
||||
for k in range(len(report)):
|
||||
report2 = report.copy()
|
||||
report2.pop(k)
|
||||
safe = loop(report2)
|
||||
if safe :
|
||||
cnt+=1
|
||||
break
|
||||
|
||||
print("Cnt = ", cnt)
|
||||
Reference in New Issue
Block a user