From ada33c13a5ee34010583dc46ba5d606345915947 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Thu, 2 Dec 2021 15:44:42 +0100 Subject: [PATCH] Add solution for 2021 day 02 --- 2021/README.md | 2 +- 2021/day-02/part_one.adb | 21 +++++++++++++++++++ 2021/day-02/part_two.adb | 26 ++++++++++++++++++++++++ 2021/day-02/shared.adb | 44 ++++++++++++++++++++++++++++++++++++++++ 2021/day-02/shared.ads | 23 +++++++++++++++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 2021/day-02/part_one.adb create mode 100644 2021/day-02/part_two.adb create mode 100644 2021/day-02/shared.adb create mode 100644 2021/day-02/shared.ads diff --git a/2021/README.md b/2021/README.md index f2bfeeb..9469830 100644 --- a/2021/README.md +++ b/2021/README.md @@ -3,7 +3,7 @@ | Solved | Day | Language | | :----: | :-: | :------- | | ✓ | 01 | Ruby | -| | 02 | | +| ✓ | 02 | Ada | | | 03 | | | | 04 | | | | 05 | | diff --git a/2021/day-02/part_one.adb b/2021/day-02/part_one.adb new file mode 100644 index 0000000..16a39ce --- /dev/null +++ b/2021/day-02/part_one.adb @@ -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; diff --git a/2021/day-02/part_two.adb b/2021/day-02/part_two.adb new file mode 100644 index 0000000..6591858 --- /dev/null +++ b/2021/day-02/part_two.adb @@ -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; diff --git a/2021/day-02/shared.adb b/2021/day-02/shared.adb new file mode 100644 index 0000000..9a3cb71 --- /dev/null +++ b/2021/day-02/shared.adb @@ -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; diff --git a/2021/day-02/shared.ads b/2021/day-02/shared.ads new file mode 100644 index 0000000..5adc553 --- /dev/null +++ b/2021/day-02/shared.ads @@ -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;