41 lines
990 B
Python
41 lines
990 B
Python
|
#!/usr/bin/env python
|
||
|
# 2024 - Advent Of Code 2 - part 2
|
||
|
|
||
|
# file = 'input_example.txt'
|
||
|
file = 'input.txt'
|
||
|
|
||
|
# pylint: disable=consider-using-with
|
||
|
input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
|
||
|
|
||
|
|
||
|
def is_safe(levels):
|
||
|
s_diff = 0 if levels[0] > levels[1] else 1
|
||
|
|
||
|
for l in range(1, len(levels)):
|
||
|
diff = levels[l] - levels[l - 1]
|
||
|
if abs(diff) > 3 or diff == 0:
|
||
|
return False
|
||
|
|
||
|
s_diff2 = 0 if levels[l] < levels[l - 1] else 1
|
||
|
if s_diff ^ s_diff2 != 0:
|
||
|
# print("sign change")
|
||
|
return False
|
||
|
|
||
|
return True
|
||
|
|
||
|
safe = 0
|
||
|
for reports in input_lines:
|
||
|
list_levels = [ int(r) for r in reports.split(' ') ]
|
||
|
|
||
|
if is_safe(list_levels):
|
||
|
safe += 1
|
||
|
else:
|
||
|
for try_level in range(0, len(list_levels)):
|
||
|
new_levels = list_levels.copy()
|
||
|
del new_levels[try_level]
|
||
|
if is_safe(new_levels):
|
||
|
safe += 1
|
||
|
break
|
||
|
|
||
|
print(f'{safe=}')
|