fork download
  1. def is_safe_report(report):
  2. """
  3. Check if a report is safe according to the rules:
  4. - All numbers are either increasing or decreasing.
  5. - The difference between adjacent numbers is between 1 and 3.
  6. """
  7. differences = []
  8. for i in range(len(report) - 1):
  9. diff = report[i+1] - report[i]
  10. if abs(diff) < 1 or abs(diff) > 3:
  11. return False # Adjacent difference not in range [1, 3]
  12. differences.append(diff)
  13.  
  14. # Check if all differences are positive (increasing) or negative (decreasing)
  15. if all(d > 0 for d in differences) or all(d < 0 for d in differences):
  16. return True
  17. return False
  18.  
  19.  
  20. def is_safe_with_dampener(report):
  21. """
  22. Check if a report is safe with the Problem Dampener.
  23. This function tests removing each level one by one to see if it results in a safe report.
  24. """
  25. # If the report is already safe, return True
  26. if is_safe_report(report):
  27. return True
  28.  
  29. # Try removing each level one by one
  30. for i in range(len(report)):
  31. modified_report = report[:i] + report[i+1:]
  32. if is_safe_report(modified_report):
  33. return True
  34.  
  35. # If no single level removal makes the report safe, return False
  36. return False
  37.  
  38.  
  39. def count_safe_reports_with_dampener(input_data):
  40. """
  41. Count the number of safe reports considering the Problem Dampener.
  42. """
  43. # Parse the input data
  44. reports = [
  45. list(map(int, line.split()))
  46. for line in input_data.strip().split("\n")
  47. ]
  48.  
  49. # Check each report for safety (with the Problem Dampener)
  50. safe_count = sum(1 for report in reports if is_safe_with_dampener(report))
  51.  
  52. return safe_count
  53.  
  54.  
  55. # Example Input (replace this with the actual input)
  56. input_data = """
  57.  
  58. """
  59.  
  60. # Calculate and print the result
  61. result = count_safe_reports_with_dampener(input_data)
  62. print("Number of safe reports with Problem Dampener:", result)
  63.  
Success #stdin #stdout 0.03s 9600KB
stdin
Standard input is empty
stdout
Number of safe reports with Problem Dampener: 1