text
stringlengths
0
234
File : File_Type;
Start_Time, End_Time : CPU_Time;
Execution_Duration : Time_Span;
File_Is_Empty : Boolean := True;
Result : Natural := Natural'First;
Nodes : Cave_Maps.Map := Cave_Maps.Empty_Map;
begin
Get_File (File);
-- Get all values
begin
while not End_Of_File (File) loop
declare
Str : constant String := Get_Line (File);
Separator_Index : constant Integer :=
Ada.Strings.Fixed.Index (Source => Str (1 .. Str'Last), Pattern => "-");
Node_1 : constant String := Str (1 .. Separator_Index - 1);
Node_2 : constant String := Str (Separator_Index + 1 .. Str'Last);
begin
declare
Cave_Node_1 : Cave_Info := (Length => Node_1'Length,
Name => Node_1,
Kind => Get_Kind (Node_1),
Adjacent => Empty_Vector);
Cave_Node_2 : Cave_Info := (Length => Node_2'Length,
Name => Node_2,
Kind => Get_Kind (Node_2),
Adjacent => Empty_Vector);
begin
if Nodes.Contains (Node_1) then
Cave_Node_1 := Nodes.Element (Node_1);
end if;
if Nodes.Contains (Node_2) then
Cave_Node_2 := Nodes.Element (Node_2);
end if;
Cave_Node_1.Adjacent.Append (Node_2);
Cave_Node_2.Adjacent.Append (Node_1);
Nodes.Include (Node_1, Cave_Node_1);
Nodes.Include (Node_2, Cave_Node_2);
Function Definition: procedure Main is
Function Body: use Ada.Execution_Time,
Ada.Real_Time,
Ada.Strings.Unbounded,
Ada.Text_IO;
use Utils;
subtype Big_Cave_Character is Character range 'A' .. 'Z';
subtype Small_Cave_Character is Character range 'a' .. 'z';
package Cave_Vectors is new Ada.Containers.Indefinite_Vectors (Index_Type => Positive,
Element_Type => String,
"=" => "=");
use Cave_Vectors;
type Cave_Kind is (K_Start, K_Big, K_Small, K_End);
Start_Name : constant String := "start";
End_Name : constant String := "end";
-- Given the name of a node, it retreive the corresponding kind
function Get_Kind (Name : String) return Cave_Kind;
--------------
-- Get_Kind --
--------------
function Get_Kind (Name : String) return Cave_Kind is
begin
if Name = Start_Name then
return K_Start;
elsif Name = End_Name then
return K_End;
elsif Name (Name'First) in Big_Cave_Character then
return K_Big;
elsif Name (Name'First) in Small_Cave_Character then
return K_Small;
end if;
raise Constraint_Error with "Unexpected cave name: " & Name;
end Get_Kind;
type Cave_Info (Length : Positive) is record
Name : String (1 .. Length);
Kind : Cave_Kind;
Adjacent : Vector;
end record;
package Cave_Maps is new Ada.Containers.Indefinite_Hashed_Maps
(Key_Type => String,
Element_Type => Cave_Info,
Hash => Ada.Strings.Hash,
Equivalent_Keys => "=");
package String_Sets is new Ada.Containers.Indefinite_Hashed_Sets (Element_Type => String,
Hash => Ada.Strings.Hash,
Equivalent_Elements => "=",
"=" => "=");