|
@@ -0,0 +1,83 @@
|
|
|
+with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
|
|
|
+with Ada.Containers.Vectors;
|
|
|
+with Ada.Containers; use Ada.Containers;
|
|
|
+
|
|
|
+with Ada.Text_IO; use Ada.Text_IO;
|
|
|
+
|
|
|
+-- This code reads its input from standard input
|
|
|
+-- Use cat _input.txt | Red_Nosed_Reports_ to get the result
|
|
|
+procedure Red_Nosed_Reports is
|
|
|
+
|
|
|
+ subtype Level is Positive;
|
|
|
+
|
|
|
+ function Reversed_Sort (Left, Right : Level) return Boolean is
|
|
|
+ begin
|
|
|
+ return Right < Left;
|
|
|
+ end Reversed_Sort;
|
|
|
+
|
|
|
+ package Report is
|
|
|
+ new Ada.Containers.Vectors (Index_type => Positive,
|
|
|
+ Element_Type => Level);
|
|
|
+ use Report;
|
|
|
+
|
|
|
+ package Report_Sorting is new Report.Generic_Sorting;
|
|
|
+ package Reverse_Report_Sorting is
|
|
|
+ new Report.Generic_Sorting ("<" => Reversed_Sort);
|
|
|
+
|
|
|
+ subtype Valid_Range is Positive range 1 .. 3;
|
|
|
+
|
|
|
+ Safe_Reports_Number : Natural := 0;
|
|
|
+begin
|
|
|
+ while not End_Of_File loop
|
|
|
+ declare
|
|
|
+ Value : Level;
|
|
|
+ Current_Report : Report.Vector;
|
|
|
+ begin
|
|
|
+ while not End_Of_Line loop
|
|
|
+ Get (Value);
|
|
|
+ Current_Report.Append (Value);
|
|
|
+ Put (Value'Image);
|
|
|
+ Put (" ");
|
|
|
+ end loop;
|
|
|
+
|
|
|
+ -- Check if safe
|
|
|
+ if Report_Sorting.Is_Sorted (Current_Report)
|
|
|
+ or else Reverse_Report_Sorting.Is_Sorted (Current_Report)
|
|
|
+ then
|
|
|
+ declare
|
|
|
+ Safe : Boolean := True;
|
|
|
+ Current_Cursor : Report.Cursor := Current_Report.First;
|
|
|
+ Previous : Level := Current_Report.First_Element;
|
|
|
+ begin
|
|
|
+ while Current_Cursor /= Current_Report.Last and then Safe loop
|
|
|
+ Next (Current_Cursor);
|
|
|
+ declare
|
|
|
+ Valid_Value : Valid_Range;
|
|
|
+ pragma Unreferenced (Valid_Value);
|
|
|
+ begin
|
|
|
+ Valid_Value := abs (Element (Current_Cursor) - Previous);
|
|
|
+ exception
|
|
|
+ when Constraint_Error => Safe := False;
|
|
|
+ end;
|
|
|
+ Previous := Element (Current_Cursor);
|
|
|
+ end loop;
|
|
|
+
|
|
|
+ if Safe then
|
|
|
+ Safe_Reports_Number := Safe_Reports_Number + 1;
|
|
|
+ Put ("Safe");
|
|
|
+ else
|
|
|
+ Put ("Unsafe");
|
|
|
+ end if;
|
|
|
+ end;
|
|
|
+ else
|
|
|
+ Put ("Unsafe");
|
|
|
+ end if;
|
|
|
+
|
|
|
+ -- Skip to next Line
|
|
|
+ Skip_Line;
|
|
|
+ end;
|
|
|
+ New_Line;
|
|
|
+ end loop;
|
|
|
+
|
|
|
+ Put_Line ("Number of Safe is " & Safe_Reports_Number'Image);
|
|
|
+end Red_Nosed_Reports;
|