2022 Day 1: Calorie Counting

[rocher][1][Ada] My solution for part 1. Quite simple, it’s just the first day…

  with Ada.Text_IO; use Ada.Text_IO;

  procedure Day01_P1 is

     Input        : File_Type;
     Calories     : Natural := 0;
     Max_Calories : Natural := 0;

     function Get_Max_Calories (K: Natural) return Natural is
       (if K > Max_Calories then K else Max_Calories);

  begin
     Open (Input, In_File, "input.txt");

     loop
        declare
           Line : String := Get_Line (Input);
        begin
           if Line'Length = 0  then
              Max_Calories := Get_Max_Calories (Calories);
              Calories := 0;
           else
              Calories := @ + Natural'Value (Line);
           end if;
        end;
        exit when End_Of_File (Input);
     end loop;
     Max_Calories := Get_Max_Calories (Calories);

     Close (Input);
     Put_Line ("Answer:" & Max_Calories'Image);
  end Day01_P1;

Here part 2 using an array:

  with Ada.Text_IO; use Ada.Text_IO;

  procedure Day01_P2 is

     Input        : File_Type;
     Calories     : Natural := 0;
     Max_Calories : array (1 .. 3) of Natural := (0, 0, 0);

     procedure Get_Max_Calories is
     begin
        for I in Max_Calories'Range loop
           if Calories > Max_Calories (I) then
              Max_Calories (I+1 .. Max_Calories'Last) := Max_Calories (I .. Max_Calories'Last-1);
              Max_Calories (I) := Calories;
              exit; -- this  loop
           end if;
        end loop;
     end Get_Max_Calories;

  begin
     Open (Input, In_File, "input.txt");

     loop
        declare
           Line : String := Get_Line (Input);
        begin
           if Line'Length = 0 then
              Get_Max_Calories;
              Calories := 0;
           else
              Calories := @ + Natural'Value (Line);
           end if;
        end;
        exit when End_Of_File (Input);
     end loop;
     Get_Max_Calories;

     Close (Input);
     Put_Line ("Answer:" & Natural'Image
                 (Max_Calories (1) + Max_Calories (2) + Max_Calories (3)));
  end Day01_P2;