red_nosed_reports.adb 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  2. with Ada.Containers.Vectors;
  3. with Ada.Containers; use Ada.Containers;
  4. with Ada.Text_IO; use Ada.Text_IO;
  5. -- This code reads its input from standard input
  6. -- Use cat _input.txt | Red_Nosed_Reports_ to get the result
  7. procedure Red_Nosed_Reports is
  8. subtype Level is Positive;
  9. function Reversed_Sort (Left, Right : Level) return Boolean is
  10. begin
  11. return Right < Left;
  12. end Reversed_Sort;
  13. package Report is
  14. new Ada.Containers.Vectors (Index_type => Positive,
  15. Element_Type => Level);
  16. use Report;
  17. package Report_Sorting is new Report.Generic_Sorting;
  18. package Reverse_Report_Sorting is
  19. new Report.Generic_Sorting ("<" => Reversed_Sort);
  20. subtype Valid_Range is Positive range 1 .. 3;
  21. Safe_Reports_Number : Natural := 0;
  22. begin
  23. while not End_Of_File loop
  24. declare
  25. Value : Level;
  26. Current_Report : Report.Vector;
  27. begin
  28. while not End_Of_Line loop
  29. Get (Value);
  30. Current_Report.Append (Value);
  31. Put (Value'Image);
  32. Put (" ");
  33. end loop;
  34. -- Check if safe
  35. if Report_Sorting.Is_Sorted (Current_Report)
  36. or else Reverse_Report_Sorting.Is_Sorted (Current_Report)
  37. then
  38. declare
  39. Safe : Boolean := True;
  40. Current_Cursor : Report.Cursor := Current_Report.First;
  41. Previous : Level := Current_Report.First_Element;
  42. begin
  43. while Current_Cursor /= Current_Report.Last and then Safe loop
  44. Next (Current_Cursor);
  45. declare
  46. Valid_Value : Valid_Range;
  47. pragma Unreferenced (Valid_Value);
  48. begin
  49. Valid_Value := abs (Element (Current_Cursor) - Previous);
  50. exception
  51. when Constraint_Error => Safe := False;
  52. end;
  53. Previous := Element (Current_Cursor);
  54. end loop;
  55. if Safe then
  56. Safe_Reports_Number := Safe_Reports_Number + 1;
  57. Put ("Safe");
  58. else
  59. Put ("Unsafe");
  60. end if;
  61. end;
  62. else
  63. Put ("Unsafe");
  64. end if;
  65. -- Skip to next Line
  66. Skip_Line;
  67. end;
  68. New_Line;
  69. end loop;
  70. Put_Line ("Number of Safe is " & Safe_Reports_Number'Image);
  71. end Red_Nosed_Reports;