1
0

historian_hysteria.adb 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
  2. with Ada.Containers.Vectors;
  3. with Ada.Containers; use Ada.Containers;
  4. with Ada.Text_IO; use Ada.Text_IO;
  5. -- This code reads its input from standard input
  6. -- Use cat _input.txt | historian_hysteria_ to get the result
  7. procedure Historian_Hysteria is
  8. subtype Location is Positive;
  9. subtype Distance is Natural;
  10. package Locations_Vectors is
  11. new Ada.Containers.Vectors (Index_type => Positive,
  12. Element_Type => Location);
  13. package Locations_Sorting is new Locations_Vectors.Generic_Sorting;
  14. Locations_1 : Locations_Vectors.Vector;
  15. Locations_2 : Locations_Vectors.Vector;
  16. Total_Distance : Distance := 0;
  17. type Input_Width_Index is new Positive range 1 .. 2;
  18. type Matrix is array (Input_Width_Index) of Locations_Vectors.Vector;
  19. Loca_Matrix : Matrix := (Locations_1, Locations_2);
  20. Position : Natural := 0;
  21. begin
  22. while not End_Of_File loop
  23. declare
  24. Value : Location;
  25. begin
  26. Position := 0;
  27. while not End_Of_Line loop
  28. Position := Position + 1;
  29. Get (Value);
  30. Loca_Matrix (Input_Width_Index (Position)).Append (Value);
  31. end loop;
  32. -- Skip to next Line
  33. Skip_Line;
  34. end;
  35. end loop;
  36. -- Order the Locations
  37. for Index in Input_Width_Index'Range loop
  38. Locations_Sorting.Sort (Loca_Matrix (Index));
  39. end loop;
  40. Put_Line ("There are " & Loca_Matrix (1).Length'Image & " values");
  41. for Index in 1 .. Natural (Loca_Matrix (1).Length) loop
  42. Total_Distance := Total_Distance
  43. + abs (Loca_Matrix (1).Element (Index)
  44. - Loca_Matrix (2).Element (Index));
  45. end loop;
  46. Put_Line ("Total distance is " & Total_Distance'Image);
  47. end Historian_Hysteria;