1
0
Fork 0
advent-of-code/2024/day02.py

41 lines
1 KiB
Python
Raw Permalink Normal View History

2024-12-03 08:43:55 +01:00
#!/usr/bin/env python3
def validate_report(levels):
inc = True
dec = True
for i in range(0, len(levels) - 1):
curr = levels[i]
nxt = levels[i + 1]
if inc and (curr == nxt or curr < nxt or curr > nxt + 3):
inc = False
if dec and (curr == nxt or curr > nxt or curr < nxt - 3):
dec = False
if inc or dec:
return True
return False
def dampened_reports(levels):
if validate_report(levels):
return True
valid_if_dampened = False
for i in range(0, len(levels)):
if validate_report(levels[:i] + levels[i + 1:]):
valid_if_dampened = True
break
return valid_if_dampened
reports = []
with open("./2024/inputs/day02.txt") as f:
reports = f.readlines()
levels = [[int(n) for n in x.strip().split(" ")] for x in reports]
safe_reports = [x for x in levels if validate_report(x)]
print(len(safe_reports))
dampened_reports = [x for x in levels if dampened_reports(x)]
print(len(dampened_reports))