123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
- with GNAT.Regpat; use GNAT.Regpat;
- with Ada.Text_IO; use Ada.Text_IO;
- with Ada.Strings.Fixed; use Ada.Strings.Fixed;
- -- This code reads its input from standard input
- -- Use cat _input.txt | Mull_It_Over_ to get the result
- procedure Mull_It_Over is
- subtype Operand is Positive;
- -- Use pattern to match mul operations
- Mul_Pattern : constant Pattern_Matcher :=
- Compile ("mul\(([0-9]+),([0-9]+)\)");
- Sum : Natural := 0;
- begin
- while not End_Of_File loop
- declare
- Line : constant String := Get_Line;
- Remaining : String := Line;
- Matches : Match_Array (0 .. Paren_Count (Mul_Pattern));
- begin
- Match (Mul_Pattern, Remaining, Matches);
- -- Iterate on all match of the line
- while Matches (0) /= No_Match loop
- declare
- Operand_1 : Operand;
- Operand_2 : Operand;
- Last : Positive;
- begin
- Get
- (Remaining (Matches (1).First .. Matches (1).Last), Operand_1,
- Last);
- Get
- (Remaining (Matches (2).First .. Matches (2).Last), Operand_2,
- Last);
- Sum := Sum + Operand_1 * Operand_2;
- end;
- -- Copy without last parenthesis
- Move
- (Remaining (Matches (2).Last + 2 .. Remaining'Last), Remaining);
- Match (Mul_Pattern, Remaining, Matches);
- end loop;
- end;
- end loop;
- Put_Line ("The sum is " & Sum'Image);
- end Mull_It_Over;
|