Skip to content

Latest commit

 

History

History
112 lines (74 loc) · 2.41 KB

06.md

File metadata and controls

112 lines (74 loc) · 2.41 KB

Binder

Day 6

Firstly, we need some standard packages to read the text file.

with Ada.Text_IO;

We represent an answer as array of Boolean.

type Answer_Array is array (Character range 'a' .. 'z') of Boolean;

A helper function to count yes reponses in an answer:

function Count (Value : Answer_Array) return Natural is
   Result : Natural := 0;
begin
   for Answer of Value loop
      if Answer then
         Result := Result + 1;
      end if;
   end loop;
      
   return Result;
end Count;

As in previous solution we are calculating both part one and part two tasks. For each group we will calculate two answers - for part 1 and part 2. On each step group answer for part 1 will extend, while for part 2 it will shrink. That's why the initialization values.

Result_1 : Natural := 0;
Result_2 : Natural := 0;

Group_Answer_1 : Answer_Array := (others => False);
Group_Answer_2 : Answer_Array := (others => True);

Let's read the file using Input:

Input : Ada.Text_IO.File_Type;

Now let's read input:

Ada.Text_IO.Open (Input, Ada.Text_IO.In_File, "/home/jovyan/md/06/input");

while not Ada.Text_IO.End_Of_File (Input) loop
   declare
      Line : String := Ada.Text_IO.Get_Line (Input);
   begin
      if Line = "" then
         Result_1 := Result_1 + Count (Group_Answer_1);
         Result_2 := Result_2 + Count (Group_Answer_2);
           
         Group_Answer_1 := (others => False);
         Group_Answer_2 := (others => True);
      else
         declare
            Current_Answer : Answer_Array := (others => False);
         begin
            for Char of Line loop
               Current_Answer (Char) := True;
            end loop;
               
            Group_Answer_1 := Group_Answer_1 or Current_Answer;
            Group_Answer_2 := Group_Answer_2 and Current_Answer;
         end;
      end if;
   end;
end loop;

Take last group answers into account:

Result_1 := Result_1 + Count (Group_Answer_1);
Result_2 := Result_2 + Count (Group_Answer_2);

Now print results for part 1 and part 2:

Ada.Text_IO.Put_Line (Result_1'Image);
Ada.Text_IO.Put_Line (Result_2'Image);
 6742
 3447

Back to Table of Contents