浏览代码

Adding Day 1 challenge

Frédéric Praca 4 月之前
当前提交
bc5fc789d5
共有 1 个文件被更改,包括 60 次插入0 次删除
  1. 60 0
      day_1/historian_hysteria.adb

+ 60 - 0
day_1/historian_hysteria.adb

@@ -0,0 +1,60 @@
+with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
+with Ada.Containers.Vectors;
+with Ada.Containers; use Ada.Containers;
+
+with Ada.Text_IO; use Ada.Text_IO;
+
+--  This code reads its input from standard input
+--  Use cat _input.txt | historian_hysteria_ to get the result
+procedure Historian_Hysteria is
+
+   subtype Location is Positive;
+   subtype Distance is Natural;
+
+   package Locations_Vectors is
+     new Ada.Containers.Vectors (Index_type => Positive,
+                                 Element_Type => Location);
+   package Locations_Sorting is new Locations_Vectors.Generic_Sorting;
+
+   Locations_1 : Locations_Vectors.Vector;
+   Locations_2 : Locations_Vectors.Vector;
+
+   Total_Distance : Distance := 0;
+
+   type Input_Width_Index is new Positive range 1 .. 2;
+
+   type Matrix is array (Input_Width_Index) of Locations_Vectors.Vector;
+
+   Loca_Matrix : Matrix := (Locations_1, Locations_2);
+
+   Position : Natural := 0;
+begin
+   while not End_Of_File loop
+      declare
+         Value : Location;
+      begin
+         Position := 0;
+         while not End_Of_Line loop
+            Position := Position + 1;
+            Get (Value);
+            Loca_Matrix (Input_Width_Index (Position)).Append (Value);
+         end loop;
+         --  Skip to next Line
+         Skip_Line;
+      end;
+   end loop;
+
+   --  Order the Locations
+   for Index in Input_Width_Index'Range loop
+      Locations_Sorting.Sort (Loca_Matrix (Index));
+   end loop;
+
+   Put_Line ("There are " & Loca_Matrix (1).Length'Image & " values");
+   for Index in 1 .. Natural (Loca_Matrix (1).Length) loop
+      Total_Distance := Total_Distance
+        + abs (Loca_Matrix (1).Element (Index)
+               - Loca_Matrix (2).Element (Index));
+   end loop;
+
+   Put_Line ("Total distance is " & Total_Distance'Image);
+end Historian_Hysteria;