Add solution for 2021 day 02

This commit is contained in:
Patrick Auernig 2021-12-02 15:44:42 +01:00
parent b429390ac2
commit ada33c13a5
5 changed files with 115 additions and 1 deletions

View File

@ -3,7 +3,7 @@
| Solved | Day | Language |
| :----: | :-: | :------- |
| ✓ | 01 | Ruby |
| | 02 | |
| ✓ | 02 | Ada |
| | 03 | |
| | 04 | |
| | 05 | |

21
2021/day-02/part_one.adb Normal file
View File

@ -0,0 +1,21 @@
with Ada.Command_Line;
with shared;
procedure part_one is
use Ada.Command_Line;
use shared;
commands : CommandVec := read_file(Argument(1));
depth : Integer := 0;
horizontal_pos : Integer := 0;
begin
for cmd of commands loop
case cmd.direction is
when Forward => horizontal_pos := horizontal_pos + cmd.distance;
when Up => depth := depth - cmd.distance;
when Down => depth := depth + cmd.distance;
end case;
end loop;
put_satanized_number_line(horizontal_pos * depth);
end part_one;

26
2021/day-02/part_two.adb Normal file
View File

@ -0,0 +1,26 @@
with Ada.Command_Line;
with shared;
procedure part_two is
use Ada.Command_Line;
use shared;
commands : CommandVec := read_file(Argument(1));
depth : Integer := 0;
aim : Integer := 0;
horizontal_pos : Integer := 0;
begin
for cmd of commands loop
case cmd.direction is
when Forward =>
horizontal_pos := horizontal_pos + cmd.distance;
depth := depth + (aim * cmd.distance);
when Up =>
aim := aim - cmd.distance;
when Down =>
aim := aim + cmd.distance;
end case;
end loop;
put_satanized_number_line(horizontal_pos * depth);
end part_two;

44
2021/day-02/shared.adb Normal file
View File

@ -0,0 +1,44 @@
with Ada.Text_IO;
with Ada.Strings.Unbounded;
package body shared is
use Ada.Text_IO;
function read_file(path : String) return CommandVec is
use Ada.Strings.Unbounded;
commands : CommandVec;
infile : File_Type;
begin
open(infile, mode => in_file, name => path);
while not end_of_file(infile) loop
declare
line : Unbounded_String;
idx : Natural;
left : Unbounded_String;
right : Unbounded_String;
direction : Directions;
distance : Natural;
cmd : Command;
begin
line := To_Unbounded_String(Get_Line(infile));
idx := Index(line, " ", 1);
left := Unbounded_Slice(line, 1, idx - 1);
right := Unbounded_Slice(line, idx + 1, Length(line));
direction := Directions'Value(To_String(left));
distance := Natural'Value(To_String(right));
cmd := (direction, distance);
commands.append(cmd);
end;
end loop;
return commands;
end read_file;
procedure put_satanized_number_line(val : in Natural) is
img : String := Natural'Image(val);
begin
put_line(img(img'First + 1 .. img'Last));
end put_satanized_number_line;
end shared;

23
2021/day-02/shared.ads Normal file
View File

@ -0,0 +1,23 @@
with Ada.Containers.Vectors;
package shared is
use Ada.Containers;
type Directions is (Forward, Up, Down);
type Command is record
direction : Directions;
distance : Natural;
end record;
package CommandVectors is new Vectors (
Index_Type => Natural,
Element_Type => Command
);
subtype CommandVec is CommandVectors.Vector;
function read_file(path : String) return CommandVec;
procedure put_satanized_number_line(val : in Natural);
end shared;