/
aoa_02_1.adb
58 lines (48 loc) · 2.46 KB
/
aoa_02_1.adb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
with Ada.Strings.Fixed;
with Ada.Text_IO;
with PragmARC.Conversions.Unbounded_Strings;
with PragmARC.Line_Fields;
procedure AOA_02_1 is
use PragmARC.Conversions.Unbounded_Strings;
type Color_ID is (Red, Green, Blue);
type Color_Count is array (Color_ID) of Natural;
Number : constant Color_Count := (Red => 12, Green => 13, Blue => 14);
Input : Ada.Text_IO.File_Type;
Sum : Natural := 0;
begin -- AOA_02_1
Ada.Text_IO.Open (File => Input, Mode => Ada.Text_IO.In_File, Name => "input_02");
All_Lines : loop
exit All_Lines when Ada.Text_IO.End_Of_File (Input);
One_Line : declare
Line : constant String := Ada.Text_IO.Get_Line (Input);
Blank : constant Natural := Ada.Strings.Fixed.Index (Line, " ");
Colon : constant Natural := Ada.Strings.Fixed.Index (Line, ":", From => Blank + 1);
ID : constant Positive := Integer'Value (Line (Blank + 1 .. Colon - 1) );
Game : constant PragmARC.Line_Fields.Line_Field_Info :=
PragmARC.Line_Fields.Parsed (Line (Colon + 1 .. Line'Last), ';');
Max : Color_Count := (others => 0);
begin -- One_Line
All_Games : for G in 1 .. Game.Field.Last_Index loop
One_Game : declare
Grab : constant PragmARC.Line_Fields.Line_Field_Info := PragmARC.Line_Fields.Parsed (+Game.Field.Element (G), ',');
begin -- One_Game
All_Colors : for I in 1 .. Grab.Field.Last_Index loop
One_Color : declare
Info : constant String := +Grab.Field.Element (I);
Space : constant Natural := Ada.Strings.Fixed.Index (Info, " ", From => 2);
Count : constant Positive := Integer'Value (Info (2 .. Space - 1) );
Color : constant Color_ID := Color_ID'Value (Info (Space + 1 .. Info'Last) );
begin -- One_Color
Max (Color) := Integer'Max (Max (Color), Count);
end One_Color;
end loop All_Colors;
end One_Game;
end loop All_Games;
if Max (Red) <= Number (Red) and Max (Green) <= Number (Green) and Max (Blue) <= Number (Blue) then
Sum := Sum + ID;
end if;
end One_Line;
end loop All_Lines;
Ada.Text_IO.Close (File => Input);
Ada.Text_IO.Put_Line (Item => Sum'Image);
end AOA_02_1;