Browse Source

Remove exception and use range

Frédéric Praca 4 months ago
parent
commit
cdf23bb19c
1 changed files with 11 additions and 12 deletions
  1. 11 12
      day_2/red_nosed_reports.adb

+ 11 - 12
day_2/red_nosed_reports.adb

@@ -10,10 +10,7 @@ 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;
+   function Reversed_Sort (Left, Right : Level) return Boolean;
 
    package Report is
      new Ada.Containers.Vectors (Index_type => Positive,
@@ -24,6 +21,11 @@ procedure Red_Nosed_Reports is
    package Reverse_Report_Sorting is
      new Report.Generic_Sorting ("<" => Reversed_Sort);
 
+   function Reversed_Sort (Left, Right : Level) return Boolean is
+   begin
+      return Right < Left;
+   end Reversed_Sort;
+
    subtype Valid_Range is Positive range 1 .. 3;
 
    Safe_Reports_Number : Natural := 0;
@@ -51,14 +53,11 @@ begin
             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;
+                  if abs (Element (Current_Cursor) - Previous)
+                     not in Valid_Range'Range
+                  then
+                     Safe := False;
+                  end if;
                   Previous := Element (Current_Cursor);
                end loop;