2022 Day 1: Calorie Counting

No worries… after all Python has strong Ada influences (it’s not by accident).

Sure, Python is everywhere. Just an example: if you have GNAT for Windows, there are no less than eight Python interpreters per GNAT version!

2 Likes

Yes!! That’s exactly the point.
Sometimes parsing input files from AOC puzzles is a bit tricky. Compared to other programming languages or libraries, the way the End_Of_File is signaled by Ada.Text_IO can cause the problem you mentioned. (e.g., in C++ eol() is returns true when you try to read pass the end, not after reading the last byte of the file)

Last year I had many problems parsing such files (and fell into the trap ;). At every puzzle I found easier ways to deal with this issue without repeating, once finished, the same logic that is performed inside the loop. For example, for me in day-1/part-1:

  loop
    declare
      Line : String := Get_Line (Input);
    begin
      if Line'Length > 0 then
        -- accumulate calories ...
      end if;

      if Line'Length = 0 or End_Of_File (Input) then
        -- calories accumulated of one elf
        -- compare with Max_Calories and get the max one
      end if;
    end;

    exit when End_Of_File (Input);
  end loop;

is the most simple and elegant way to process the file. I don’t like (and always avoid) repeating the same logic after the loop just because the way the file is read or the loop is structured.

Yes, that’s the correct answer. Please see next post, @zertovitch found the trap :wink: