2024 - Day 2 part 2
This commit is contained in:
parent
e14adff2cd
commit
beef70848d
1 changed files with 40 additions and 0 deletions
40
2024/2/2_2.py
Normal file
40
2024/2/2_2.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/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=}')
|
Loading…
Reference in a new issue