/
day1_2.adb
84 lines (73 loc) · 1.75 KB
/
day1_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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
with Advent_IO.Integers; use Advent_IO.Integers;
with Advent_IO; use Advent_IO;
with Chests.Ring_Buffers;
procedure Day1_2 is
package Integer_Buffers is new Chests.Ring_Buffers (Integer, 16);
use Integer_Buffers;
Nums : Ring_Buffer;
function Match
(Sub : String)
return Boolean
is
begin
if Lookahead (Input, Sub'Length) = Sub then
-- Seek (Input, Sub'Length);
Seek (Input, 1);
return True;
else
return False;
end if;
end Match;
procedure Number
(N : Positive)
is
begin
Append (Nums, N);
end Number;
Sum : Natural := 0;
procedure Accumulate is
N : Natural;
begin
N := First_Element (Nums) * 10;
N := N + Last_Element (Nums);
Sum := Sum + N;
Clear (Nums);
end Accumulate;
Ch : Character;
begin
Clear (Nums);
while not End_Of_Input loop
Ch := Peek (Input);
if Ch = ASCII.CR or else Ch = ASCII.LF then
Seek (Input, 1);
Accumulate;
elsif Ch in '1' .. '9' then
Append (Nums, Character'Pos (Ch) - Character'Pos ('0'));
Seek (Input, 1);
elsif Match ("one") then
Number (1);
elsif Match ("two") then
Number (2);
elsif Match ("three") then
Number (3);
elsif Match ("four") then
Number (4);
elsif Match ("five") then
Number (5);
elsif Match ("six") then
Number (6);
elsif Match ("seven") then
Number (7);
elsif Match ("eight") then
Number (8);
elsif Match ("nine") then
Number (9);
else
-- skip
Seek (Input, 1);
end if;
end loop;
Accumulate;
Put (Output, Sum);
New_Line (Output);
end Day1_2;