-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.adb
157 lines (130 loc) · 5.73 KB
/
day3.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
with Ada.Containers;
with Ada.Text_IO;
with GNAT.String_Split;
with Coord;
procedure Day3
with SPARK_Mode => On
is
procedure String_To_Vector
(Input_Split_1 : GNAT.String_Split.Slice_Set;
Input_Split_2 : GNAT.String_Split.Slice_Set)
is
type Wire_1_Vecs_T is array
(Natural range 0 .. Natural
(GNAT.String_Split.Slice_Count (Input_Split_1)) - 1) of Coord.Vector;
type Wire_2_Vecs_T is array
(Natural range 0 .. Natural
(GNAT.String_Split.Slice_Count (Input_Split_2)) - 1) of Coord.Vector;
Wire_1_Vecs : Wire_1_Vecs_T;
Wire_1_Len : Natural := 0;
Wire_2_Vecs : Wire_2_Vecs_T;
Wire_2_Len : Natural := 0;
procedure Manhattan_Wires
(Wire_1_Vecs : Wire_1_Vecs_T;
Wire_1_Len : Natural;
Wire_2_Vecs : Wire_2_Vecs_T;
Wire_2_Len : Natural)
is
Wire_1 : Coord.Coord_Points.Vector ;--(Ada.Containers.Count_Type(Wire_1_Len));
Wire_2 : Coord.Coord_Points.Vector ;--(Ada.Containers.Count_Type(Wire_2_Len));
Pos : Coord.Coord;
Distance : Natural;
Steps : Natural;
Smallest_Distance : Natural := Natural'Last;
Smallest_Steps : Natural := Natural'Last;
Elem : Coord.Coord;
begin
Pos := Coord.Start_Coord;
Coord.Coord_Points.Append(Wire_1, Pos);
for Vec of Wire_1_Vecs loop
Coord.Expand_Vec (Vec, Wire_1, Pos);
end loop;
Ada.Text_IO.Put_Line("Wire 1 Created" & Coord.Coord_Points.Length(Wire_1)'Image);
Pos := Coord.Start_Coord;
Coord.Coord_Points.Append(Wire_2, Pos);
Ada.Text_IO.Put_Line("Wire 2 Append" & Coord.Coord_Points.Length(Wire_1)'Image);
for Vec of Wire_2_Vecs loop
Coord.Expand_Vec (Vec, Wire_2, Pos);
end loop;
Ada.Text_IO.Put_Line("Wire 1 Created" & Coord.Coord_Points.Length(Wire_1)'Image);
Ada.Text_IO.Put_Line("Wire 2 Created" & Coord.Coord_Points.Length(Wire_2)'Image);
for J in Coord.Coord_Points.First_Index (Wire_1) ..
Coord.Coord_Points.Last_Index (Wire_1) loop
--Ada.Text_IO.Put_Line("W1" & J'Image & Coord.Coord_Points.Element(Wire_1, J).Y'Image);
Elem := Coord.Coord_Points.Element(Wire_1, J);
if Coord.Coord_Points.Contains(Wire_2, Elem)
then
Distance := abs(Elem.X) + abs(Elem.Y);
Ada.Text_IO.Put_Line("FOUND" & Distance'Image);
if Distance > 0 and then Distance < Smallest_Distance then
Smallest_Distance := Distance;
end if;
Steps := J + Natural
(Coord.Coord_Points.Find_Index (Wire_2, Elem));
Ada.Text_IO.Put_Line("STEPS" & Steps'Image);
if Steps > 0 and then Steps < Smallest_Steps then
Smallest_Steps := Steps;
end if;
end if;
end loop;
-- for J in Coord.Coord_Points.First_Index (Wire_2) ..
-- Coord.Coord_Points.Last_Index (Wire_2) loop
-- null;
-- -- Ada.Text_IO.Put_Line("W2" & J'Image & Coord.Coord_Points.Element(Wire_2, J).Y'Image);
-- end loop;
Ada.Text_IO.Put_Line("Fin:" & Coord.Coord_Points.Length(Wire_1)'Image);
Ada.Text_IO.Put_Line("Smallest Distance:" & Smallest_Distance'Image);
Ada.Text_IO.Put_Line("Smallest Steps:" & Smallest_Steps'Image);
end Manhattan_Wires;
begin
-- This is bloody messy. Could be made into a generic.
for I in Wire_1_Vecs'Range loop
declare
String_Vector : constant String := (GNAT.String_Split.Slice
(Input_Split_1, GNAT.String_Split.Slice_Number(I + 1)));
begin
Wire_1_Vecs (I) := Coord.Vector'
(Direction => Coord.Direction_T'Value
(String_Vector (String_Vector'First .. String_Vector'First)),
Size => Natural'Value
(String_Vector (String_Vector'First + 1 .. String_Vector'Last)));
Wire_1_Len := Wire_1_Len + Wire_1_Vecs (I).Size;
end;
end loop;
for I in Wire_2_Vecs'Range loop
declare
String_Vector : constant String := (GNAT.String_Split.Slice
(Input_Split_2, GNAT.String_Split.Slice_Number(I + 1)));
begin
Wire_2_Vecs (I) := Coord.Vector'
(Direction => Coord.Direction_T'Value
(String_Vector (String_Vector'First .. String_Vector'First)),
Size => Natural'Value
(String_Vector (String_Vector'First + 1 .. String_Vector'Last)));
Wire_2_Len := Wire_2_Len + Wire_2_Vecs (I).Size;
end;
end loop;
Ada.Text_IO.Put_Line(Wire_1_Vecs(0).Direction'Image & Wire_1_Vecs(0).Size'Image);
Ada.Text_IO.Put_Line(Wire_2_Vecs(0).Direction'Image & Wire_2_Vecs(0).Size'Image);
Manhattan_Wires(Wire_1_Vecs, Wire_1_Len, Wire_2_Vecs, Wire_2_Len);
end String_To_Vector;
Input_Split_1 : GNAT.String_Split.Slice_Set;
Input_Split_2 : GNAT.String_Split.Slice_Set;
begin
loop
declare
Line_1 : constant String := Ada.Text_IO.Get_Line;
Line_2 : constant String := Ada.Text_IO.Get_Line;
begin
GNAT.String_Split.Create(From => Line_1,
Separators => ",",
S => Input_Split_1);
GNAT.String_Split.Create(From => Line_2,
Separators => ",",
S => Input_Split_2);
String_To_Vector(Input_Split_1, Input_Split_2);
exit when Ada.Text_IO.End_Of_File;
end;
end loop;
null;
end Day3;