Advent of Ada Submissions

[AJ-Ianozi][7][Ada]adventofcode/day7.adb at ac386d423726060ce0e5b9e1416601d2c8d6da9d · AJ-Ianozi/adventofcode · GitHub

[JeremyGrosser][8][Ada][advent/day8_2.adb at bbd3a860f27775ee4f794cf390af74fe69b26230 · JeremyGrosser/advent · GitHub]

[cantanima][8][Ada]Treetop Tree House

[Zertovitch][8][Ada]hac/aoc_2022_08.adb at c416e7e50709181da178436ff9b6fb30d5267e61 · zertovitch/hac · GitHub

[rommudoh][8][Ada] aoc2022-Ada/day08.adb at main - aoc2022-Ada - Codeberg.org

[rocher][8][Ada] Treetop Tree House

part 1:

pragma Ada_2022;

with Ada.Text_IO; use Ada.Text_IO;

procedure Day08_P1 is

    subtype Tree_Range is Positive range 1 .. 99;
    type Forest_Type is array (Tree_Range, Tree_Range) of Character;

    Forest    : Forest_Type;
    Σ_Visible : Natural    := Tree_Range'Last * 4 - 4;
    Input     : File_Type;
    T         : Tree_Range := Tree_Range'First;

    function Is_Edge (X, Y : Tree_Range) return Boolean is
       (X = Tree_Range'First or else X = Tree_Range'Last
        or else Y = Tree_Range'First or else Y = Tree_Range'Last);

    function Is_Visible_From
       (X, Y : Tree_Range; Δx, Δy : Integer) return Boolean
    is
        P : Tree_Range := X + Δx;
        Q : Tree_Range := Y + Δy;
    begin
        while Forest (X, Y) > Forest (P, Q) loop
            if Is_Edge (P, Q) then
                return True;
            else
                P := @ + Δx;
                Q := @ + Δy;
            end if;
        end loop;
        return False;
    end Is_Visible_From;

    function Is_Visible (X, Y : Tree_Range) return Boolean is
       (Is_Visible_From (X, Y, 1, 0) or else Is_Visible_From (X, Y, -1, 0)
        or else Is_Visible_From (X, Y, 0, 1)
        or else Is_Visible_From (X, Y, 0, -1));

begin
    Open (Input, In_File, "input.txt");

    loop
        declare
            Line : String := Get_Line (Input);
        begin
            for I in Tree_Range loop
                Forest (T, I) := Line (I);
            end loop;
        end;
        exit when End_Of_File (Input);
        T := T + 1;
    end loop;

    for X in Tree_Range'First + 1 .. Tree_Range'Last - 1 loop
        for Y in Tree_Range'First + 1 .. Tree_Range'Last - 1 loop
            Σ_Visible := @ + (if Is_Visible (X, Y) then 1 else 0);
        end loop;
    end loop;

    Close (Input);
    Put_Line ("Answer:" & Σ_Visible'Image);
end Day08_P1;

Part 2:

pragma Ada_2022;

with Ada.Text_IO; use Ada.Text_IO;

procedure Day08_P2 is

    subtype Tree_Range is Positive range 1 .. 99;
    type Forest_Type is array (Tree_Range, Tree_Range) of Character;

    Forest    : Forest_Type;
    Max_Score : Natural    := 0;
    Input     : File_Type;
    T         : Tree_Range := Tree_Range'First;

    function Is_Out (X, Y : Natural) return Boolean is
       (X < Tree_Range'First or else X > Tree_Range'Last
        or else Y < Tree_Range'First or else Y > Tree_Range'Last);

    function Visibility (X, Y : Tree_Range; Δx, Δy : Integer) return Natural is
        P            : Natural := X + Δx;
        Q            : Natural := Y + Δy;
        Σ_Trees      : Natural := 0;
        View_Blocked : Boolean := False;
    begin
        loop
            exit when View_Blocked or Is_Out (P, Q);
            if Forest (X, Y) > Forest (P, Q) then
                P := @ + Δx;
                Q := @ + Δy;
            else
                View_Blocked := True;
            end if;
            Σ_Trees := @ + 1;
        end loop;
        return Σ_Trees;
    end Visibility;

    function Scenic_Score (X, Y : Tree_Range) return Natural is
       (Visibility (X, Y, 1, 0) * Visibility (X, Y, -1, 0) *
        Visibility (X, Y, 0, 1) * Visibility (X, Y, 0, -1));

begin
    Open (Input, In_File, "input.txt");

    loop
        declare
            Line : String := Get_Line (Input);
        begin
            for I in Tree_Range loop
                Forest (T, I) := Line (I);
            end loop;
        end;
        exit when End_Of_File (Input);
        T := T + 1;
    end loop;

    for X in Tree_Range loop
        for Y in Tree_Range loop
            Max_Score := Natural'Max (@, Scenic_Score (X, Y));
        end loop;
    end loop;

    Close (Input);
    Put_Line ("Answer:" & Max_Score'Image);
end Day08_P2;
1 Like

[anuj-seth][3][Ada]Day 3

[wutka][8][SPARK]advent-of-code-2022-spark/day8.adb at 4d3629a147aa6e78bb7589c2e41036acf481aad7 · wutka/advent-of-code-2022-spark · GitHub

[zetah11][7][SPARK][aoc2022/day7.adb at 70d654b014dbe46333a871d0366a5ff3427cb90c · zetah11/aoc2022 · GitHub]
[zetah11][8][SPARK][aoc2022/day8.adb at 70d654b014dbe46333a871d0366a5ff3427cb90c · zetah11/aoc2022 · GitHub]
[zetah11][9][SPARK][aoc2022/day9.adb at 9781ea6bdff0dfbea3d803e7bfc38a08d08cc745 · zetah11/aoc2022 · GitHub]
[zetah11][10][SPARK][aoc2022/day10.adb at a773423ab9f6464ae7e1f39c2f24e7dcf0362a91 · zetah11/aoc2022 · GitHub]

[Jeff Carter][6][Ada]AoA_22/day06_1.adb at 63d605033da1cf312d346de6c78f7ae76f33b67f · jrcarter/AoA_22 · GitHub

[William_Franck][8][Ada]Release Day-08 · captain-haddock17/Advent_of_Code_2022 · GitHub

[AJ-Ianozi][8][Ada]adventofcode/day8.adb at a3ac816b50d682e16ed09f0f6843e2761b379f21 · AJ-Ianozi/adventofcode · GitHub

1 Like

[smionean][8][Ada]AdventOfCode/day08.adb at 47ba1116065ec890d5bb09634d95d1316d962c7f · smionean/AdventOfCode · GitHub

[cantanima][9][Ada]Rope Bridge

1 Like

[JeremyGrosser][9][Ada][advent/day9_2.adb at 5ee36f43f289c6d4842ad65f6e3e8c314d4e3953 · JeremyGrosser/advent · GitHub]

[Zertovitch][9][Ada]hac/aoc_2022_09.adb at 414ed82db19b67070c7c699ce0bfb128fa9bcb71 · zertovitch/hac · GitHub

[rocher][9][Ada]

Part 2 only (part 1 is a particular case):

pragma Ada_2022;

with Ada.Text_IO;                    use Ada.Text_IO;
with Ada.Integer_Text_IO;            use Ada.Integer_Text_IO;
with Ada.Containers.Functional_Maps; use Ada.Containers;

procedure Day09_P2 is

    type Position_Type is record
        X, Y : Integer;
    end record;

    type Rope_Type is array (1 .. 10) of Position_Type;

    function "=" (P, Q : Position_Type) return Boolean is
       (P.X = Q.X and then P.Y = Q.Y);

    package Position_Container is new Functional_Maps
       (Key_Type        => Position_Type, Element_Type => Integer,
        Equivalent_Keys => "=", "=" => "=");
    use Position_Container;

    Grid        : Position_Container.Map;
    Knot        : Rope_Type := (others => (0, 0));
    Σ_Positions : Natural   := 1;
    Input       : File_Type;

    function Are_Aligned (P, Q : Position_Type) return Boolean is
       (P.X = Q.X or P.Y = Q.Y);

    function Horizontal_Distance (P, Q : Position_Type) return Natural is
       (abs (P.X - Q.X));

    function Vertical_Distance (P, Q : Position_Type) return Natural is
       (abs (P.Y - Q.Y));

    function Distance (P, Q : Position_Type) return Natural is
       (Horizontal_Distance (P, Q) + Vertical_Distance (P, Q));

    procedure Update_Knots (Head : Position_Type; Tail : in out Position_Type)
    is
        Δ : Integer;
    begin
        if Distance (Head, Tail) > 2
           or else (Distance (Head, Tail) = 2 and Are_Aligned (Head, Tail))
        then
            if Horizontal_Distance (Head, Tail) >= 1 then
                Δ    := (if Head.X > Tail.X then 1 else -1);
                Tail := (@.X + Δ, @.Y);
            end if;
            if Vertical_Distance (Head, Tail) >= 1 then
                Δ    := (if Head.Y > Tail.Y then 1 else -1);
                Tail := (@.X, @.Y + Δ);
            end if;
        end if;
    end Update_Knots;

    procedure Update_Grid (Tail : Position_Type) is
    begin
        if not Has_Key (Grid, Tail) then
            Grid        := Add (Grid, Tail, 1);
            Σ_Positions := @ + 1;
        end if;
    end Update_Grid;

    procedure Update_Head (Head : in out Position_Type; Direction : Character)
    is
    begin
        case Direction is
            when 'U' =>
                Head := (@.X, @.Y + 1);
            when 'L' =>
                Head := (@.X - 1, @.Y);
            when 'D' =>
                Head := (@.X, @.Y - 1);
            when 'R' =>
                Head := (@.X + 1, @.Y);
            when others =>
                null;
        end case;
    end Update_Head;

    procedure Move_Head (Direction : Character; Length : Natural) is
    begin
        for I in 1 .. Length loop
            Update_Head (Knot (Knot'First), Direction);
            for K in Knot'First .. Knot'Last - 1 loop
                Update_Knots (Knot (K), Knot (K + 1));
            end loop;
            Update_Grid (Knot (Knot'Last));
        end loop;
    end Move_Head;

begin
    Grid        := Add (Grid, Knot (Knot'Last), 1);
    Σ_Positions := 1;

    Open (Input, In_File, "input.txt");

    loop
        declare
            Direction : Character;
            Length    : Natural;
        begin
            Get (Input, Direction);
            Get (Input, Length);
            Move_Head (Direction, Length);
        end;
        exit when End_Of_File (Input);
    end loop;

    Close (Input);

    Put_Line ("Answer:" & Σ_Positions'Image);
end Day09_P2;

[rommudoh][9][Ada] aoc2022-Ada/day09.adb at main - aoc2022-Ada - Codeberg.org

[jklmnn][4][SPARK] aoc/day_2022_4.adb at 25afb337056e867ae05aa5aa7cbaf599d75f37c7 · jklmnn/aoc · GitHub

[RREE][8][Ada] AoC/2022/08 at main · RREE/AoC · GitHub, part 1 only
[RREE][9][Ada] AoC/2022/09 at main · RREE/AoC · GitHub