aoa_22_20220720.0.0_6d9ebd71/src/day02_2.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
59
60
61
62
63
64
65
66
67
68
69
70
with Ada.Text_IO;

procedure Day02_2 is
   subtype Elf_Shape_ID is Character range 'A' .. 'C'; -- A => Rock, B => Paper, C => Scissors
   subtype My_Shape_ID  is Character range 'X' .. 'Z'; -- X     '    Y      "    Z       "
   subtype Shape_Value is Integer range 1 .. 3;
   subtype Score_Value is Integer range 0 .. 6 with Dynamic_Predicate => Score_Value in 0 | 3 | 6;

   Lose : constant Score_Value := 0;
   Draw : constant Score_Value := 3;
   Win  : constant Score_Value := 6;

   function Shape_Map (Elf_Shape : in Elf_Shape_ID; Match_Result : in My_Shape_ID) return Shape_Value;

   type Match_Score is array (My_Shape_ID) of Score_Value;

   function Shape_Map (Elf_Shape : in Elf_Shape_ID; Match_Result : in My_Shape_ID) return Shape_Value is
      -- Empty
   begin -- Shape_Map
      case Match_Result is
      when 'X' => -- Lose
         case Elf_Shape is
         when 'A' => -- Rock
            return 3; -- Scissors
         when 'B' => -- Paper
            return 1; -- Rock
         when 'C' => -- Scissors
            return 2; -- Paper;
         end case;
      when 'Y' => -- Draw
         case Elf_Shape is
         when 'A' => -- Rock
            return 1; -- Rock
         when 'B' => -- Paper
            return 2; -- Paper
         when 'C' => -- Scissors
            return 3; -- Scissors;
         end case;
      when 'Z' => -- Win
         case Elf_Shape is
         when 'A' => -- Rock
            return 2; -- Paper
         when 'B' => -- Paper
            return 3; -- Scissors
         when 'C' => -- Scissors
            return 1; -- Rock;
         end case;
      end case;
   end Shape_Map;

   Match_Map : constant Match_Score := ('X' => Lose, 'Y' => Draw, 'Z' => Win);

   Input : Ada.Text_IO.File_Type;
   Score : Natural := 0;
begin -- Day02_2
   Ada.Text_IO.Open (File => Input, Mode => Ada.Text_IO.In_File, Name => "input_02");

   All_Matches : loop
      exit All_Matches when Ada.Text_IO.End_Of_File (Input);

      One_Match : declare
         Line : constant String := Ada.Text_IO.Get_Line (Input);
      begin -- One_Match
         Score := Score + Shape_Map (Line (Line'First), Line (Line'First + 2) ) + Match_Map (Line (Line'First + 2) );
      end One_Match;
   end loop All_Matches;

   Ada.Text_IO.Close (File => Input);
   Ada.Text_IO.Put_Line (Item => Score'Image);
end Day02_2;