-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoa_15_1.adb
54 lines (42 loc) · 1.72 KB
/
aoa_15_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
with Ada.Text_IO;
with PragmARC.Conversions.Unbounded_Strings;
with PragmARC.Line_Fields;
procedure AOA_15_1 is
use PragmARC.Conversions.Unbounded_Strings;
function Sum_Hash (Line : in String) return Natural;
-- Divides Line into substrings separated by commas, runs the HASH algorithm on each substring, and returns the sum of the
-- hashes
function Sum_Hash (Line : in String) return Natural is
function Hash (Line : in String) return Natural;
-- Returns the HASH algorithm result for Line
function Hash (Line : in String) return Natural is
type Hash_Value is mod 256;
Result : Hash_Value := 0;
begin -- Hash
All_Chars : for C of Line loop
Result := 17 * (Result + Character'Pos (C) );
end loop All_Chars;
return Integer (Result);
end Hash;
Field : constant PragmARC.Line_Fields.Line_Field_Info := PragmARC.Line_Fields.Parsed (Line, ',');
Sum : Natural := 0;
begin -- Sum_Hash
All_Fields : for I in 1 .. Field.Field.Last_Index loop
Sum := Sum + Hash (+Field.Field.Element (I) );
end loop All_Fields;
return Sum;
end Sum_Hash;
Test_Input : constant String := "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7";
Test_Result : constant := 1320;
Input : Ada.Text_IO.File_Type;
Sum : Natural;
begin -- AOA_15_1
Sum := Sum_Hash (Test_Input);
Ada.Text_IO.Put_Line (Item => Sum'Image);
if Sum = Test_Result then
Ada.Text_IO.Open (File => Input, Mode => Ada.Text_IO.In_File, Name => "input_15");
Sum := Sum_Hash (Ada.Text_IO.Get_Line (Input) );
Ada.Text_IO.Close (File => Input);
Ada.Text_IO.Put_Line (Item => Sum'Image);
end if;
end AOA_15_1;