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 count_safe_reports(input_data):
  21. """
  22. Count the number of safe reports in the input data.
  23. """
  24. # Parse the input data
  25. reports = [
  26. list(map(int, line.split()))
  27. for line in input_data.strip().split("\n")
  28. ]
  29.  
  30. # Check each report for safety
  31. safe_count = sum(1 for report in reports if is_safe_report(report))
  32.  
  33. return safe_count
  34.  
  35.  
  36. # Example Input (replace this with the actual input)
  37. input_data = """
  38.  
  39. """
  40.  
  41. # Calculate and print the result
  42. result = count_safe_reports(input_data)
  43. print("Number of safe reports:", result)
  44.  
Success #stdin #stdout 0.03s 9628KB
stdin
Standard input is empty
stdout
Number of safe reports: 1