-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDecember_03a.adb
100 lines (84 loc) · 2.89 KB
/
December_03a.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
with Ada.Text_IO; use Ada.Text_IO;
procedure December_03a is
-- 147 142 133 122 59
-- 304 5 4 2 57
-- 330 10 1 1 54
-- 351 11 23 25 26
-- 362 747 806---> ...
Max_Coordinate : constant Integer := 1000;
subtype Bounds is integer range 0 .. Max_Coordinate;
subtype Coordinates is integer range - Max_Coordinate .. Max_Coordinate;
subtype Values is Natural;
Grid : array (Coordinates, Coordinates) of Values;
Current_Bound : Bounds := 0;
X, Y : Coordinates := 0; -- address 1 lives here (0, 0)
Penultimate_Value : Values;
package Value_IO is new Integer_IO (Values);
package Coordinate_IO is new Integer_IO (Coordinates);
function Value_Of ( X, Y : in Coordinates; Current_Bound : in Bounds)
return Values is
Value : Values := 0;
begin -- Value_Of
if X + 1 <= Current_Bound then
Value := Value + Grid (X + 1, Y);
if Y + 1 <= Current_Bound then
Value := Value + Grid (X + 1, Y + 1);
end if;
if Y - 1 >= -Current_Bound then
Value := Value + Grid (X + 1, Y - 1);
end if;
end if; -- X + 1 <= Current_Bound
if X - 1 >= - Current_Bound then
Value := Value + Grid (X - 1, Y);
if Y + 1 <= Current_Bound then
Value := Value + Grid (X - 1, Y + 1);
end if;
if Y - 1 >= -Current_Bound then
Value := Value + Grid (X - 1, Y - 1);
end if;
end if; -- X - 1 >= - Current_Bound
if Y + 1 <= Current_Bound then
Value := Value + Grid (X, Y + 1);
end if;
if Y - 1 >= -Current_Bound then
Value := Value + Grid (X, Y - 1);
end if;
return Value;
end Value_Of;
begin -- December_03a
for X in Coordinates loop
for Y in Coordinates loop
Grid (X, Y) := 0;
end loop;
end loop;
Grid (0, 0) := 1; -- don't know how to initialise this otherwise
Put ("Value: ");
Value_IO.Get (Penultimate_Value);
while Penultimate_Value >= Grid (X, Y) loop
if X = Current_Bound and Y = - Current_Bound then
-- bottom right corner has been reached
Current_Bound := Current_Bound + 1;
X := Current_Bound;
elsif X = Current_Bound and Y < Current_Bound then
-- Step up
Y := Y + 1;
elsif Y = Current_Bound and X > - Current_Bound then
-- step left
X := X - 1;
elsif X = - Current_Bound and Y > - Current_Bound then
-- step down
Y := Y - 1;
elsif Y = - Current_Bound and X < Current_Bound then
-- step right
X := X + 1;
end if;
Grid (X, Y) := Value_Of (X, Y, Current_Bound);
end loop; -- Penultimate_Value >= Grid (X, Y)
Put ( '(');
Coordinate_IO.Put (X);
Put ( ',' );
Coordinate_IO.Put (Y);
Put ( ") Value: ");
Value_IO.Put (Grid (X, Y));
New_Line;
end December_03a;