aoc24/day2/solution.py

23 lines
637 B
Python

def is_safe(report):
levels = list(map(int, report.split()))
differences = [levels[i+1] - levels[i] for i in range(len(levels) - 1)]
if all(-3 <= diff <= -1 for diff in differences) or all(1 <= diff <= 3 for diff in differences):
return True
return False
def count_safe_reports(data):
reports = data.strip().split('\n')
safe_count = sum(is_safe(report) for report in reports)
return safe_count
data = """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"""
data = open('input.txt', 'r').read()
safe_reports = count_safe_reports(data)
print(f"Number of safe reports: {safe_reports}")