Add solutions for day 2 part 1 and part 2
This commit is contained in:
parent
4dbe06279f
commit
0c2302b3a5
105
AoC/2015/day_2/day_2.adb
Normal file
105
AoC/2015/day_2/day_2.adb
Normal file
|
|
@ -0,0 +1,105 @@
|
||||||
|
with Ada.Text_IO; use Ada.Text_IO;
|
||||||
|
with Ada.Containers.Vectors;
|
||||||
|
with Ada.Strings; use Ada.Strings;
|
||||||
|
with Ada.Strings.Fixed; use Ada.Strings.Fixed;
|
||||||
|
|
||||||
|
procedure Day_2 is
|
||||||
|
|
||||||
|
type Box is
|
||||||
|
record
|
||||||
|
Width : Natural;
|
||||||
|
Length : Natural;
|
||||||
|
Height : Natural;
|
||||||
|
end record;
|
||||||
|
|
||||||
|
type Box_Array is array (Natural range <>) of Box;
|
||||||
|
|
||||||
|
function Read_File( File_Name : String ) return Box_Array is
|
||||||
|
package vec is new Ada.Containers.Vectors(
|
||||||
|
Natural, Box);
|
||||||
|
|
||||||
|
use vec;
|
||||||
|
|
||||||
|
Box_Vector : vec.Vector;
|
||||||
|
|
||||||
|
type locations is array (Natural range <>) of Natural;
|
||||||
|
|
||||||
|
X : locations(0 .. 1);
|
||||||
|
|
||||||
|
File : File_Type;
|
||||||
|
begin
|
||||||
|
|
||||||
|
open(File, In_File, File_Name);
|
||||||
|
|
||||||
|
while not End_Of_File(File) loop
|
||||||
|
declare
|
||||||
|
Line : String := Trim(Get_Line(File), Both);
|
||||||
|
B : Box;
|
||||||
|
Index : Natural := X'First;
|
||||||
|
begin
|
||||||
|
for I in Line'Range loop
|
||||||
|
if Line(I) = 'x' then
|
||||||
|
X(Index) := I;
|
||||||
|
Index := @ + 1;
|
||||||
|
end if;
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
B.Length := Natural'Value(Line(Line'First .. X(0) - 1));
|
||||||
|
B.Width := Natural'Value(Line(X(0) + 1 .. X(1) - 1));
|
||||||
|
B.Height := Natural'Value(Line(X(1) + 1 .. Line'Last));
|
||||||
|
|
||||||
|
Box_Vector.Append(B);
|
||||||
|
end;
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
close(File);
|
||||||
|
|
||||||
|
return Ret : Box_Array( 0 .. Natural(Box_Vector.Length) - 1) do
|
||||||
|
for I in Ret'Range loop
|
||||||
|
Ret(I) := Box_Vector(I);
|
||||||
|
end loop;
|
||||||
|
end return;
|
||||||
|
|
||||||
|
end Read_File;
|
||||||
|
|
||||||
|
function Day_2_1( Boxs : Box_Array ) return Natural is
|
||||||
|
Sum : Natural := 0;
|
||||||
|
begin
|
||||||
|
for I in Boxs'Range loop
|
||||||
|
declare
|
||||||
|
Area_1 : Natural := Boxs(I).Length * Boxs(I).Width;
|
||||||
|
Area_2 : Natural := Boxs(I).Width * Boxs(I).Height;
|
||||||
|
Area_3 : Natural := Boxs(I).Height * Boxs(I).Length;
|
||||||
|
begin
|
||||||
|
Sum := @ + 2 * Area_1 + 2 * Area_2 + 2 * Area_3;
|
||||||
|
Sum := @ + Natural'Min(Area_1, Natural'Min(Area_2, Area_3));
|
||||||
|
end;
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
return Sum;
|
||||||
|
end Day_2_1;
|
||||||
|
|
||||||
|
function Day_2_2( Boxs : Box_Array ) return Natural is
|
||||||
|
Sum : Natural := 0;
|
||||||
|
begin
|
||||||
|
for I in Boxs'Range loop
|
||||||
|
declare
|
||||||
|
B : Box := Boxs(I);
|
||||||
|
Peri_1 : Natural := 2*(B.Width+B.Length);
|
||||||
|
Peri_2 : Natural := 2*(B.Length+B.Height);
|
||||||
|
Peri_3 : Natural := 2*(B.Height+B.Width);
|
||||||
|
begin
|
||||||
|
Sum := @ + B.Width*B.Length*B.Height;
|
||||||
|
Sum := @ + Natural'Min(Peri_1, Natural'Min(Peri_2, Peri_3));
|
||||||
|
end;
|
||||||
|
end loop;
|
||||||
|
|
||||||
|
return Sum;
|
||||||
|
end Day_2_2;
|
||||||
|
|
||||||
|
begin
|
||||||
|
|
||||||
|
Put_Line(Day_2_1(Read_File("input_day_2.txt"))'Image);
|
||||||
|
Put_Line(Day_2_2(Read_File("input_day_2.txt"))'Image);
|
||||||
|
|
||||||
|
end Day_2;
|
||||||
Loading…
Reference in New Issue
Block a user