Skip to content

Instantly share code, notes, and snippets.

@de-wim
Created December 12, 2025 17:55
Show Gist options
  • Select an option

  • Save de-wim/4b152c216b15e5b6f14fcf126850b017 to your computer and use it in GitHub Desktop.

Select an option

Save de-wim/4b152c216b15e5b6f14fcf126850b017 to your computer and use it in GitHub Desktop.
day5.adb
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Containers.Vectors;
with Ada.Containers.Generic_Sort;
with Ada.Strings; use Ada.Strings;
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
procedure Day5 is
type FreshRange is record
Start : Long_Long_Integer;
Stop : Long_Long_Integer;
end record;
package RangeList is new Ada.Containers.Vectors(
Index_Type => Natural,
Element_Type => FreshRange
);
function ParseRange ( Input_String : String ) return FreshRange is
Result : FreshRange;
Dash_Idx : Natural;
begin
Dash_Idx := Index(Input_String, "-");
if Dash_Idx > 0 then
begin
Result.Start := Long_Long_Integer'Value(Input_String(Input_String'First..Dash_Idx - 1));
Result.Stop := Long_Long_Integer'Value(Input_String(Dash_Idx + 1..Input_String'Last));
exception
when Constraint_Error =>
Put_Line("Invalid format: Substrings are not valid integers.");
end;
end if;
return Result;
end ParseRange;
File : File_Type;
Line : String(1..80);
Length : Natural;
Ranges : RangeList.Vector;
Fresh_Count : Natural := 0;
begin
Open(File, In_File, "input/5.txt");
while not End_Of_File(File) loop
-- Read a line from the file
Get_Line(File, Line, Length);
-- Break out of the loop if the line is empty
if Length = 0 then
exit;
end if;
Ranges.Append(ParseRange(Line(Line'First..Length)));
end loop;
while not End_Of_File(File) loop
Get_Line(File, Line, Length);
begin
declare
Ingredient : Long_Long_Integer;
begin
Ingredient := Long_Long_Integer'Value(Line(Line'First..Length));
if not Ranges.Is_Empty then
for I in Ranges.First_Index .. Ranges.Last_Index loop
declare
Current_Range : FreshRange := Ranges(i);
begin
if Ingredient >= Current_Range.Start and Ingredient <= Current_Range.Stop then
Fresh_Count := Fresh_Count + 1;
exit;
end if;
end;
end loop;
end if;
end;
exception
when Constraint_Error =>
Put_Line("Invalid ingredient " & Line(Line'First..Length));
end;
end loop;
Put_Line(Fresh_Count'Image);
end Day5;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment