33 lines
1,008 B
Python
33 lines
1,008 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 with_dampener_safe(report):
|
|
levels = report.split()
|
|
if is_safe(report):
|
|
return True
|
|
for i in range(len(levels)):
|
|
modified_report = ' '.join(levels[:i] + levels[i+1:])
|
|
if is_safe(modified_report):
|
|
return True
|
|
return False
|
|
|
|
def count_safe_reports_with_dampener(data):
|
|
reports = data.strip().split('\n')
|
|
safe_count = sum(with_dampener_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_with_dampener = count_safe_reports_with_dampener(data)
|
|
print(f"Number of safe reports with the Problem Dampener: {safe_reports_with_dampener}")
|
|
|