text
stringlengths
0
234
package Location_Maps is new Ada.Containers.Hashed_Maps (Key_Type => Location,
Element_Type => Risk_Level,
Hash => Hash,
Equivalent_Keys => "=",
"=" => "=");
use Location_Maps;
Adjacents : constant Adjacent_Array := (Top => (-1, 0),
Right => (0, 1),
Bottom => (1, 0),
Left => (0, -1));
function Equivalent_Keys (Left, Right : Location) return Boolean is
(Left.Line = Right.Line and Left.Column = Right.Column);
-- Display the location
procedure Put (Loc : Location);
---------
-- Put --
---------
procedure Put (Loc : Location) is
begin
Put ("(");
Ada.Integer_Text_IO.Put (Item => Loc.Line,
Width => 0);
Put (",");
Ada.Integer_Text_IO.Put (Item => Loc.Column,
Width => 0);
Put (")");
end Put;
package Chiton_Dijkstra is new Dijkstra (Node => Location,
Hash => Hash,
Equivalent_Keys => Equivalent_Keys,
Put => Put,
Is_Directed => True);
-- This heuristic compute the Manhattan distance from Current to To
function Heuristic (Current, To : Location) return Long_Long_Natural;
function Heuristic (Current, To : Location) return Long_Long_Natural is
begin
return Long_Long_Natural (abs (To.Line - Current.Line) + abs (To.Column - Current.Column));
end Heuristic;
use Chiton_Dijkstra;
File : File_Type;
Start_Time, End_Time : CPU_Time;
Execution_Duration : Time_Span;
Array_Width,
Array_Height : Natural := Natural'First;
Result : Long_Long_Natural := Long_Long_Natural'First;
Nodes : Map;
Edges : Edges_Lists.Vector := Edges_Lists.Empty_Vector;
Graph : Graph_Access;
begin
Get_File (File);
-- Get all values
while not End_Of_File (File) loop
declare
Str : constant String := Get_Line (File);
Value : Risk_Level;
Last : Positive;
Current_Colum : Natural := Natural'First;
begin
if Array_Width = Natural'First then
Array_Width := Str'Length;
end if;
for Char of Str loop
Ada.Integer_Text_IO.Get (Char & "", Value, Last);
Nodes.Insert ((Array_Height, Current_Colum), Value);
Current_Colum := Current_Colum + 1;
end loop;
Create_Horizontal_Tiles : for Column in Array_Width .. Array_Width * Nb_Tiles - 1 loop
Last := Nodes.Element ((Array_Height, Column - Array_Width)) + 1;
if Last > 9 then
Last := Last - 9;
end if;
Nodes.Insert ((Array_Height, Column), Last);
end loop Create_Horizontal_Tiles;
Function Definition: procedure Main is
Function Body: use Ada.Execution_Time,
Ada.Real_Time,
Ada.Text_IO;
use Utils;
Nb_Tiles : constant := 5;
subtype Risk_Level is Natural range 1 .. 9;
type Location is record
Line : Natural;
Column : Natural;