# Advent of Ada Submissions

**URL:** https://forum.ada-lang.io/t/advent-of-ada-submissions/129
**Category:** Advent of Code
**Created:** [November 24, 2022, 12:47pm UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129 "2022-11-24T12:47:13Z")
**Posts on this page:** 20
**Page:** 6

<div class="post-metadata">

### Author: ![rocher](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rocher/32/89_2.png) [@rocher](https://forum.ada-lang.io/u/rocher)
#### Post date: [December 6, 2022, 6:52am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/101 "2022-12-06T06:52:19Z")

</div>

[rocher][6][Ada] Delete only one character to get Part 1.

Part 2 here:

```ada
pragma Ada_2022;

with Ada.Text_IO; use Ada.Text_IO;

procedure Day06_P2 is

    subtype Marker_Type is String (1 .. 14);

    function Is_Marker (M : Marker_Type) return Boolean is
    begin
        for X in M'First .. M'Last - 1 loop
            for Y in X + 1 .. M'Last loop
                if M (X) = M (Y) then
                    return False;
                end if;
            end loop;
        end loop;
        return True;
    end Is_Marker;

    procedure Insert (M : in out Marker_Type; C : Character) is
    begin
        M (M'First .. M'Last - 1) := M (M'First + 1 .. M'Last);
        M (M'Last) := C;
    end Insert;

    Marker : Marker_Type;
    Next_Char : Character;
    Position : Natural := Marker'Last;
    Input : File_Type;

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

    Get (Input, Marker);
    while not Is_Marker (Marker) loop
        Get (Input, Next_Char);
        Insert (Marker, Next_Char);
        Position := @ + 1;
    end loop;

    Close (Input);
    Put_Line ("Answer:" & Position'Image);
end Day06_P2;

```

---

<div class="post-metadata">

### Author: ![rommudoh](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rommudoh/32/101_2.png) [@rommudoh](https://forum.ada-lang.io/u/rommudoh)
#### Post date: [December 6, 2022, 8:36am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/102 "2022-12-06T08:36:19Z")

</div>

[rommudoh][6][Ada] [aoc2022-Ada/day06.adb at main - aoc2022-Ada - Codeberg.org](https://codeberg.org/rommudoh/aoc2022-Ada/src/branch/main/src/day06.adb)

Maybe someone could pin this topic, so it appears at the top of the category?

---

<div class="post-metadata">

### Author: ![zetah11](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/z/58f4c7/32.png) [@zetah11](https://forum.ada-lang.io/u/zetah11)
#### Post date: [December 6, 2022, 9:36am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/103 "2022-12-06T09:36:16Z")

</div>

[zetah11][5][Ada][[aoc2022/day5.adb at 89da3ebd570d2361944c3c3fcc0d0a2ff24e2531 · zetah11/aoc2022 · GitHub](https://github.com/zetah11/aoc2022/blob/89da3ebd570d2361944c3c3fcc0d0a2ff24e2531/day5/src/day5.adb)]  
[zetah11][6][SPARK][[aoc2022/day6.adb at 89da3ebd570d2361944c3c3fcc0d0a2ff24e2531 · zetah11/aoc2022 · GitHub](https://github.com/zetah11/aoc2022/blob/89da3ebd570d2361944c3c3fcc0d0a2ff24e2531/day6/src/day6.adb)]

---

<div class="post-metadata">

### Author: ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)
#### Post date: [December 6, 2022, 10:21am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/104 "2022-12-06T10:21:16Z")

</div>

[Jeff Carter][3][Ada][AoA\_22/day03\_1.adb at 180485c719b9b84a30217ef05f06b4a6954e7deb · jrcarter/AoA\_22 · GitHub](https://github.com/jrcarter/AoA_22/blob/180485c719b9b84a30217ef05f06b4a6954e7deb/day03_1.adb)

---

<div class="post-metadata">

### Author: ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)
#### Post date: [December 6, 2022, 11:02am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/105 "2022-12-06T11:02:40Z")

</div>

[Jeff Carter][4][Ada][AoA\_22/day04\_1.adb at 2918c968207dc2d432b5dbf8c288c35f29151598 · jrcarter/AoA\_22 · GitHub](https://github.com/jrcarter/AoA_22/blob/2918c968207dc2d432b5dbf8c288c35f29151598/day04_1.adb)

---

<div class="post-metadata">

### Author: ![captain-haddock17](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/captain-haddock17/32/186_2.png) [@captain-haddock17](https://forum.ada-lang.io/u/captain-haddock17)
#### Post date: [December 6, 2022, 5:15pm UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/106 "2022-12-06T17:15:24Z")

</div>

[William-Franck][6][Ada][Release Day-06 · captain-haddock17/Advent\_of\_Code\_2022 · GitHub](https://github.com/captain-haddock17/Advent_of_Code_2022/releases/tag/Day-06)

---

<div class="post-metadata">

### Author: ![JeremyGrosser](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jeremygrosser/32/3_2.png) [@JeremyGrosser](https://forum.ada-lang.io/u/JeremyGrosser)
#### Post date: [December 6, 2022, 6:03pm UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/107 "2022-12-06T18:03:10Z")

</div>



---

<div class="post-metadata">

### Author: ![dkm](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/d/ce73a5/32.png) [@dkm](https://forum.ada-lang.io/u/dkm)
#### Post date: [December 6, 2022, 9:27pm UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/108 "2022-12-06T21:27:13Z")

</div>

[dkm][5][Ada] [git code](https://github.com/dkm/adventofcode/blob/07e7bd86ffe73b3ba8639a8c3260619cc57cd2fb/2022/ada/day5/src/main.adb) [execute](https://godbolt.org/z/hK5WMGnPY)

---

<div class="post-metadata">

### Author: ![AJ-Ianozi](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/aj-ianozi/32/39_2.png) [@AJ-Ianozi](https://forum.ada-lang.io/u/AJ-Ianozi)
#### Post date: [December 6, 2022, 10:24pm UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/109 "2022-12-06T22:24:29Z")

</div>

[AJ-Ianozi][6][Ada][adventofcode/day6.adb at ebb302485402a10835bd8976c867616e3bd50b93 · AJ-Ianozi/adventofcode · GitHub](https://github.com/AJ-Ianozi/adventofcode/blob/ebb302485402a10835bd8976c867616e3bd50b93/2022/src/day6.adb)

---

<div class="post-metadata">

### Author: ![smionean](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/smionean/32/88_2.png) [@smionean](https://forum.ada-lang.io/u/smionean)
#### Post date: [December 7, 2022, 2:16am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/110 "2022-12-07T02:16:59Z")

</div>

[smionean][6][Ada][AdventOfCode/day06.adb at 274c38e3335a8f2c5a86295838fbb55757a12545 · smionean/AdventOfCode · GitHub](https://github.com/smionean/AdventOfCode/blob/274c38e3335a8f2c5a86295838fbb55757a12545/2022/day06/src/day06.adb)

---

<div class="post-metadata">

### Author: ![zertovitch](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/zertovitch/32/90_2.png) [@zertovitch](https://forum.ada-lang.io/u/zertovitch)
#### Post date: [December 7, 2022, 6:42am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/111 "2022-12-07T06:42:50Z")

</div>

[Zertovitch][7][Ada][hac/aoc\_2022\_07.adb at d4ae791493b5016fa65ff123601309cf08f56123 · zertovitch/hac · GitHub](https://github.com/zertovitch/hac/blob/d4ae791493b5016fa65ff123601309cf08f56123/exm/aoc/2022/aoc_2022_07.adb)

---

<div class="post-metadata">

### Author: ![jklmnn](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jklmnn/32/108_2.png) [@jklmnn](https://forum.ada-lang.io/u/jklmnn)
#### Post date: [December 7, 2022, 8:14am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/112 "2022-12-07T08:14:16Z")

</div>

[jklmnn][3][SPARK] [aoc/day\_2022\_3.adb at 4ca30c79e2d904a2d00161d93f60a90e79b029db · jklmnn/aoc · GitHub](https://github.com/jklmnn/aoc/blob/4ca30c79e2d904a2d00161d93f60a90e79b029db/Ada/src/day_2022_3.adb)

---

<div class="post-metadata">

### Author: ![rommudoh](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rommudoh/32/101_2.png) [@rommudoh](https://forum.ada-lang.io/u/rommudoh)
#### Post date: [December 7, 2022, 8:40am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/113 "2022-12-07T08:40:17Z")

</div>

[rommudoh][7][Ada] [aoc2022-Ada/day07.adb at main - aoc2022-Ada - Codeberg.org](https://codeberg.org/rommudoh/aoc2022-Ada/src/branch/main/src/day07.adb)

---

<div class="post-metadata">

### Author: ![rocher](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rocher/32/89_2.png) [@rocher](https://forum.ada-lang.io/u/rocher)
#### Post date: [December 7, 2022, 8:49am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/114 "2022-12-07T08:49:42Z")

</div>

[rocher][7][Ada] Here parts 1 & 2, using a mix between trees and lists

```ada
pragma Ada_2022;

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Containers.Doubly_Linked_Lists; use Ada.Containers;

procedure Day07_P1 is

    type Node_Type;
    type Node_Access is access all Node_Type;

    package Node_Package is new Doubly_Linked_Lists (Node_Access);
    use Node_Package;

    subtype Node_Name is Unbounded_String;
    function To_Node_Name (Str : String) return Node_Name renames
       To_Unbounded_String;

    type Subdirectory is access all Node_Package.List;

    type Node_Type is record
        parent : Node_Access := null;
        name : Node_Name := Null_Unbounded_String;
        size : Natural := 0;
        dir : Subdirectory := null;
    end record;

    procedure Set_File_Size (Node : Node_Access; Size : Natural) is
        Parent : Node_Access := Node.parent;
    begin
        Node.size := Size;

        while Parent /= null loop
            Parent.size := @ + Size;
            Parent := Parent.parent;
        end loop;
    end Set_File_Size;
    -- Set file size and propagate the size (add) to parent directories

    procedure Add_Node
       (Node : Node_Access; Name : Node_Name; Size : Natural := 0)
    is
        New_Node : Node_Access := new Node_Type;
    begin
        New_Node.parent := Node;
        New_Node.name := Name;

        if Node.dir = null then
            Node.dir := new Node_Package.List;
        end if;
        Node.dir.Append (New_Node);

        if Size > 0 then
            Set_File_Size (New_Node, Size);
        end if;
    end Add_Node;
    -- Append a new node to the subdirectory

    function Change_Directory
       (CWD : Node_Access; Name : Node_Name) return Node_Access
    is
        Dir : Node_Package.Cursor := CWD.dir.First;
    begin
        while Element (Dir).name /= Name loop
            Dir := Next (Dir);
        end loop;

        return Element (Dir);
    end Change_Directory;
    -- Change from CWD to directory 'Name'

    function Select_and_Sum (Node : Node_Access) return Natural is
        Child : Node_Package.Cursor;
        Σ_size : Natural := 0;
    begin
        if Node.dir /= null then
            if Node.size <= 100_000 then
                Σ_size := @ + Node.size;
            end if;

            Child := Node.dir.First;
            while Child /= Node_Package.No_Element loop
                Σ_size := @ + Select_and_Sum (Element (Child));
                Child := Next (Child);
            end loop;
        end if;

        return Σ_size;
    end Select_and_Sum;
    -- Returns the sum of the directories with size <= 100_000

    function Select_Smallest
       (Node : Node_Access := null; Used : Natural := 0; Min : Natural := 0)
        return Natural
    is
        Child : Node_Package.Cursor;
        Used_Size : Natural := Used;
        Min_Size : Natural := Min;
    begin
        if Node.name = To_Node_Name ("/") then
            Used_Size := Node.size;
            Min_Size := Node.size;
        end if;

        if Node.dir /= null then
            if 70_000_000 + Node.size - Used_Size >= 30_000_000 and
               Node.size < Min_Size
            then
                Min_Size := Node.size;
            end if;

            Child := Node.dir.First;
            while Child /= Node_Package.No_Element loop
                Min_Size :=
                   Select_Smallest (Element (Child), Used_Size, Min_Size);
                Child := Next (Child);
            end loop;
        end if;
        return Min_Size;
    end Select_Smallest;
    -- Returns the size of the smallest directory that provides enough space

    Root : Node_Access := new Node_Type;
    CWD : Node_Access := Root; -- current working directory
    Input : File_Type;

begin
    Root.name := To_Node_Name ("/");
    Open (Input, In_File, "input.txt");

    loop
        declare
            Line : String := Get_Line (Input);
        begin
            if Line (1) = '$' then -- new command
                if Line (3 .. 4) = "cd" then
                    declare
                        Name : Node_Name :=
                           To_Node_Name (Line (6 .. Line'Last));
                    begin
                        if Name = "/" then
                            CWD := Root;
                        elsif Name = ".." then
                            CWD := CWD.parent;
                        else
                            CWD := Change_Directory (CWD, Name);
                        end if;
                    end;
                else
                    null; -- start new 'ls' command
                end if;
            else -- continue with current 'ls' command
                declare
                    Space : Natural := Index (To_Unbounded_String (Line), " ");
                    Size : Natural := 0;
                    Name : Node_Name :=
                       To_Node_Name (Line (Space + 1 .. Line'Last));
                begin
                    if Line (1) in '0' .. '9' then
                        Size := Natural'Value (Line (1 .. Space - 1));
                    end if;
                    Add_Node (CWD, Name, Size);
                end;
            end if;
        end;
        exit when End_Of_File (Input);
    end loop;

    Close (Input);

    Put_Line ("Answer part 1:" & Natural'Image (Select_and_Sum (Root)));
    Put_Line ("Answer part 2:" & Natural'Image (Select_Smallest (Root)));
end Day07_P1;

```

---

<div class="post-metadata">

### Author: ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)
#### Post date: [December 7, 2022, 9:19am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/115 "2022-12-07T09:19:20Z")

</div>

[cantanima][7][Ada][Used Multiway\_Trees, which was great once I read the problem correctly](https://github.com/johnperry-math/AoC2022/tree/master/Day7)

---

<div class="post-metadata">

### Author: ![JeremyGrosser](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jeremygrosser/32/3_2.png) [@JeremyGrosser](https://forum.ada-lang.io/u/JeremyGrosser)
#### Post date: [December 7, 2022, 10:00am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/116 "2022-12-07T10:00:51Z")

</div>

[JeremyGrosser][7][Ada][[advent/day7\_2.adb at 070eb5528caecf78faad5bb5da8e17d036220ec1 · JeremyGrosser/advent · GitHub](https://github.com/JeremyGrosser/advent/blob/070eb5528caecf78faad5bb5da8e17d036220ec1/2022/src/day7_2.adb)]

---

<div class="post-metadata">

### Author: ![rocher](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rocher/32/89_2.png) [@rocher](https://forum.ada-lang.io/u/rocher)
#### Post date: [December 7, 2022, 10:04am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/117 "2022-12-07T10:04:04Z")

</div>

Good! I was not aware of this container, will try to adapt my solution soon.

---

<div class="post-metadata">

### Author: ![dkm](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/d/ce73a5/32.png) [@dkm](https://forum.ada-lang.io/u/dkm)
#### Post date: [December 7, 2022, 11:13am UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/118 "2022-12-07T11:13:07Z")

</div>

[dkm][6][Ada] [Compiler Explorer](https://godbolt.org/z/rj3jWcEf6) [git code](https://github.com/dkm/adventofcode/tree/main/2022/ada/day6/src)  
[dkm][7][Ada] [Compiler Explorer](https://godbolt.org/z/dMo3r68fb) [git code](https://github.com/dkm/adventofcode/blob/main/2022/ada/day7/src/main.adb)

---

<div class="post-metadata">

### Author: ![wutka](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/wutka/32/854_2.png) [@wutka](https://forum.ada-lang.io/u/wutka)
#### Post date: [December 7, 2022, 4:28pm UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/119 "2022-12-07T16:28:09Z")

</div>

[wutka][7][SPARK] [advent-of-code-2022-spark/day7.adb at 42f81d3db5c4d93d9d2dac7f7028eeccf9be1618 · wutka/advent-of-code-2022-spark · GitHub](https://github.com/wutka/advent-of-code-2022-spark/blob/42f81d3db5c4d93d9d2dac7f7028eeccf9be1618/day7/src/day7.adb)

The permalink above points to my original solution, but then reading about reznikmm’s solution with a recursive parser, I redid it just using an array, and it still uses SPARK mode:

[https://github.com/wutka/advent-of-code-2022-spark/blob/main/day7/src/day7.adb](https://github.com/wutka/advent-of-code-2022-spark/blob/main/day7/src/day7.adb)

---

<div class="post-metadata">

### Author: ![JC001](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/j/e56c9b/32.png) [@JC001](https://forum.ada-lang.io/u/JC001)
#### Post date: [December 7, 2022, 11:05pm UTC](https://forum.ada-lang.io/t/advent-of-ada-submissions/129/120 "2022-12-07T23:05:49Z")

</div>

[Jeff Carter[5][Ada][AoA\_22/day05\_1.adb at b38b96dc0b3f6f7f4c1da3e9f9ca0b80accf6c59 · jrcarter/AoA\_22 · GitHub](https://github.com/jrcarter/AoA_22/blob/b38b96dc0b3f6f7f4c1da3e9f9ca0b80accf6c59/day05_1.adb)

[Previous page](https://forum.ada-lang.io/t/advent-of-ada-submissions/129.md?page=5)

[Next page](https://forum.ada-lang.io/t/advent-of-ada-submissions/129.md?page=7)
