Advent of Ada Submissions

[rocher][5][Ada]

I use Double_Linked_Lists, but I miss a subprogram to concatenate to lists. Is there a better way to do part 2? without using a temporary list?

Part 1:

pragma Ada_2022;

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

procedure Day05_P1 is

    package Crate_Stack is new Ada.Containers.Doubly_Linked_Lists (Character);
    use Crate_Stack;

    Num_Stacks : constant Natural := 9;

    Tops  : String (1 .. Num_Stacks);
    Stack : array (1 .. Num_Stacks) of Crate_Stack.List;
    Input : File_Type;

    procedure Read_Stacks is
        Finished : Boolean := False;
    begin
        loop
            declare
                Pos  : Natural := 2;
                Line : String  := Get_Line (Input);
            begin
                if Line (Pos) /= '1' then
                    for I in 1 .. Num_Stacks loop
                        if Line (Pos) in 'A' .. 'Z' then
                            Stack (I).Append (Line (Pos));
                        end if;
                        Pos := @ + 4; -- jump to next stack
                    end loop;
                else
                    Finished := True;
                end if;
            end;
            exit when Finished;
        end loop;
    end Read_Stacks;

    procedure Move (Qty, From, To : Natural) is
    begin
        for I in 1 .. Qty loop
            Stack (To).Prepend (Stack (From).First_Element);
            Stack (From).Delete_First;
        end loop;
    end Move;

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

    Read_Stacks;
    loop
        declare
            Separator     : String (1 .. 5);
            Qty, From, To : Natural;
        begin
            Get (Input, Separator);          -- skip 'move '
            Get (Input, Qty);
            Get (Input, Separator);          -- skip ' from'
            Get (Input, From);
            Get (Input, Separator (1 .. 4)); -- skip ' to '
            Get (Input, To);
            Move (Qty, From, To);
        end;
        exit when End_Of_File (Input);
    end loop;

    Close (Input);

    for I in 1 .. Num_Stacks loop
        if Stack (I).Is_Empty then
            Tops (I) := ' ';
        else
            Tops (I) := Stack (I).First_Element;
        end if;
    end loop;

    Put_Line ("Answer: " & Tops);
end Day05_P1;

Part 2:

pragma Ada_2022;

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

procedure Day05_P2 is

    package Crate_Stack is new Ada.Containers.Doubly_Linked_Lists (Character);
    use Crate_Stack;

    Num_Stacks : constant Natural := 9;

    Tops  : String (1 .. Num_Stacks);
    Stack : array (1 .. Num_Stacks) of Crate_Stack.List;
    Input : File_Type;

    procedure Read_Stacks is
        Finished : Boolean := False;
    begin
        loop
            declare
                Pos  : Natural := 2;
                Line : String  := Get_Line (Input);
            begin
                if Line (Pos) /= '1' then
                    for I in 1 .. Num_Stacks loop
                        if Line (Pos) in 'A' .. 'Z' then
                            Stack (I).Append (Line (Pos));
                        end if;
                        Pos := @ + 4; -- jump to next stack
                    end loop;
                else
                    Finished := True;
                end if;
            end;
            exit when Finished;
        end loop;
    end Read_Stacks;

    procedure Move_Stack (Qty, From, To : Natural) is
        Temp : Crate_Stack.List;
    begin
        for I in 1 .. Qty loop
            Temp.Prepend (Stack (From).First_Element);
            Stack (From).Delete_First;
        end loop;

        for I in 1 .. Qty loop
            Stack (To).Prepend (Temp.First_Element);
            Temp.Delete_First;
        end loop;
    end Move_Stack;

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

    Read_Stacks;
    loop
        declare
            Separator     : String (1 .. 5);
            Qty, From, To : Natural;
        begin
            Get (Input, Separator);          -- skip 'move '
            Get (Input, Qty);
            Get (Input, Separator);          -- skip ' from'
            Get (Input, From);
            Get (Input, Separator (1 .. 4)); -- skip ' to '
            Get (Input, To);
            Move_Stack (Qty, From, To);
        end;
        exit when End_Of_File (Input);
    end loop;

    Close (Input);

    for I in 1 .. Num_Stacks loop
        if Stack (I).Is_Empty then
            Tops (I) := ' ';
        else
            Tops (I) := Stack (I).First_Element;
        end if;
    end loop;

    Put_Line ("Answer: " & Tops);
end Day05_P2;

I see 59 solutions in Ada and 23 in SPARK so far!
An impressive start :sunglasses:

3 Likes

[Fabien-Chouteau][2][Ada] 2022/src/day_02.adb

[rommudoh][5][Ada] aoc2022-Ada/day05.adb at main - aoc2022-Ada - Codeberg.org

I don’t know about “better”, but I used arrays instead of vectors, and in there’s no need to use a temporary array, because Ada copies parts of arrays quite easily. I basically used something equivalent to, Last_2 .. Last_2 + Num) := Array_1(Last_1 - Num .. Last_1); and then adjusted Last_1 and Last_2 accordingly. I’m not sure if you can do the same with Ada’s vectors.

[wutka][5][SPARK]advent-of-code-2022-spark/day5.adb at cdf5e10990980f85df4632a35e2c084ad973fbde · wutka/advent-of-code-2022-spark · GitHub
[wutka][6][SPARK]advent-of-code-2022-spark/day6.adb at 9ac68610acb8399da85eae085db052b32fdbec06 · wutka/advent-of-code-2022-spark · GitHub

[William-Franck][5][Ada]Release Day-05 · captain-haddock17/Advent_of_Code_2022 · GitHub

Plain Ada here, using traditional push, pop, insert of elements in a stack (pile) with access type.
see puzzle_05

[dkm][3][Ada] adventofcode/main.adb at 53077dbff42a7e64de02e13e80c5d4e92af8899b · dkm/adventofcode · GitHub (yes I’m starting to lag behind only at day 3 :rofl:)

Can’t edit my previous post :woman_shrugging:
[analogrelay][5][Ada] aoc2022/day_05.adb at efdc692f8021c74a9ae0199c41edf7e8af4ca89b · analogrelay/aoc2022 · GitHub

[AJ-Ianozi][5][Ada]adventofcode/day5.adb at 2430624770b2a6f1cc430322af197817ef25f750 · AJ-Ianozi/adventofcode · GitHub

[dkm][4][Ada] adventofcode/main.adb at 7a19ca90b92a8a030f579991c409f273e00c55ff · dkm/adventofcode · GitHub

—o— Kind Reminder —o—

Feel free to join the Advent of Ada Leaderboard.
Visit Leaderboard - Advent of Code 2022 and use the code 1708445-6a8f7730 to join us.

This is the current status … and growing!!

1 Like

[RREE][5][Ada] AoC/2022/05 at main · RREE/AoC · GitHub

[fsherratt][5][Ada]GitHub - fsherratt/Advent_Of_Code_2022
[fsherratt][6][SPARK]GitHub - fsherratt/Advent_Of_Code_2022

[smionean][5][Ada]AdventOfCode/day05.adb at c6e0be7723b5dc7fe140d8fb8ecf116bba71a74f · smionean/AdventOfCode · GitHub

[JeremyGrosser][6][Ada][advent/day6_2.adb at b34668e635541721b73cce9d7dd6c5d8866695bb · JeremyGrosser/advent · GitHub]

[Zertovitch][6][Ada]hac/aoc_2022_06.adb at 560c7cebafeae9e84107e372733a999d05498267 · zertovitch/hac · GitHub

Edited, style-checked, and, as usual, integrated to the HAC compiler test suite.

[analogrelay][6][Ada] aoc2022/day_06.adb at d5d2366b411cc19a18be889a2685799d77e35557 · analogrelay/aoc2022 · GitHub

[cantanima][6][SPARK]Day 6: Tuning Trouble

If anyone has advice on how to create a postcondition that guarantees the result of Find_Marker has distinct characters, I’d love to hear it. I know how to write one, but getting SPARK to prove it took more effort than I have time to give at the moment.