# 2024 Day 1: Historian Hysteria

**URL:** <https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523>\
**Category:** Advent of Code\
**Created:** [December 1, 2024, 5:20am UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523 "2024-12-01T05:20:38Z")\
**Posts on this page:** 16\
**Page:** 1

<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 1, 2024, 5:20am UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/1 "2024-12-01T05:20:38Z")

</div>

I went with Vectors for the first bit, and an Ordered\_Map for the second part. I’d forgotten how to use Generic\_Sorting, so that took me a few minutes to read through the RM, but not too bad.

Definitely not an optimal solution, curious to see if anybody did it with arrays or trees.

[advent/2024/src/day1\_1.adb at 261d2fd25cf21b545c60fc69c77869b2f522a8fb · JeremyGrosser/advent · GitHub](https://github.com/JeremyGrosser/advent/blob/261d2fd25cf21b545c60fc69c77869b2f522a8fb/2024/src/day1_1.adb)  
[advent/2024/src/day1\_2.adb at 261d2fd25cf21b545c60fc69c77869b2f522a8fb · JeremyGrosser/advent · GitHub](https://github.com/JeremyGrosser/advent/blob/261d2fd25cf21b545c60fc69c77869b2f522a8fb/2024/src/day1_2.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 1, 2024, 8:19am UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/2 "2024-12-01T08:19:38Z")

</div>

Used the means available with HAC (that is, not a lot…):

> <https://github.com/zertovitch/hac/commit/1e444b9>

---

<div class="post-metadata">

**Author:** ![Max](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/max/32/21_2.png) [@Max](https://forum.ada-lang.io/u/Max)\
**Post date:** [December 1, 2024, 10:07am UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/3 "2024-12-01T10:07:23Z")

</div>

I repackaged Ada 83 frontend into [alire crate](https://github.com/reznikmm/diana) and processed my [day 1 solution](https://github.com/reznikmm/advent-of-ada/blob/6d7753a6218513d3d554f4e0babb91d487526da8/source/day_01/day_01.adb) with it. I was wondering how functional it is. Maybe it makes sense to make a toy Ada 83 compiler based on it. I couldn’t get through Vincent’s code. 🤡

---

<div class="post-metadata">

**Author:** ![Irvise](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/i/7ba0ec/32.png) [@Irvise](https://forum.ada-lang.io/u/Irvise)\
**Post date:** [December 1, 2024, 10:12am UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/4 "2024-12-01T10:12:05Z")

</div>

I added my opinion in the main thread, but I think this thread will be better:

First impression… This is the first time I have used `Vectors`. I did not know that for the input type definition one has to do `Instantiated_Vector_Package_Name.Vector` and not just `Instantiated_Vector_Package_Name`. It makes sense after all, but the compiler was not useful at all! I kept saying `subtype mark error vector` but I just did not understand it…  
Additionally, iterating over two vectors with the same index was not as clean as I had hoped… I wanted to do a `for Val_1 of Vec_1` and `for Val_1 of Vec_1` but that does not work so cleanly… So direct indexing `for I in Vec_1.First_Index..Vec_1.Last_Index` was the way to go. And this also shows the issue of `tagged` types vs normal types IMHO… One cannot do a simple `Vec_1'Range`…

My solution is quite dirty too, but it works. I was very happy to gain the gold star after compiling it once and running it 🙂 That is the power of Ada (and it was also a simple problem too 😛 ) Though it could have been greatly improved if I made use of the fact that the vectors are sorted…

Overall, it took me less that I expected, more than it should have 😛

Best,  
Fer

Solution: [advent-of-code-2024/day\_1 at main · Irvise/advent-of-code-2024 · GitHub](https://github.com/Irvise/advent-of-code-2024/tree/main/day_1)

---

<div class="post-metadata">

**Author:** ![alexispaez](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/alexispaez/32/920_2.png) [@alexispaez](https://forum.ada-lang.io/u/alexispaez)\
**Post date:** [December 1, 2024, 10:59am UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/5 "2024-12-01T10:59:00Z")

</div>

My small contribution 🙂

Used two vectors and generic vector sorting. The trickiest bit was indexing both vectors at the same time, but it wasn’t too hard 🙂

[alexispaez][01][Ada][advent-of-code/2024/day\_1\_1/src/day\_1\_1.adb at 701d5f09cdbaa53a54003172e1a647b244136e61 · alexispaez/advent-of-code · GitHub](https://github.com/alexispaez/advent-of-code/blob/701d5f09cdbaa53a54003172e1a647b244136e61/2024/day_1_1/src/day_1_1.adb)

[alexispaez][01][Ada][advent-of-code/2024/day\_1\_2/src/day\_1\_2.adb at master · alexispaez/advent-of-code · GitHub](https://github.com/alexispaez/advent-of-code/blob/master/2024/day_1_2/src/day_1_2.adb)

Added part 2 as well.

---

<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 1, 2024, 3:50pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/6 "2024-12-01T15:50:55Z")

</div>

Solved it in a very similar way as everyone else, except I took advantage of the Ada 2022 feature of filtering `for` loops to count the vector:

```ada
function Count (List : ID_List.Vector; Item : Integer)
   return Integer
is
   Result : Integer := 0;
begin
   for I of List when I = Item loop -- Using "when" so no if
      Result := @ + 1;
   end loop;
   return Result;
end Count;

```

Here’s the github link: [aj-ianozi][01][Ada]

[adventofcode/2024/day\_01/src/day\_01.adb at ccbf89f04d06f768f44fd4f845fda09e351bb50f · AJ-Ianozi/adventofcode · GitHub](https://github.com/AJ-Ianozi/adventofcode/blob/ccbf89f04d06f768f44fd4f845fda09e351bb50f/2024/day_01/src/day_01.adb)

---

<div class="post-metadata">

**Author:** ![tgv](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/tgv/32/548_2.png) [@tgv](https://forum.ada-lang.io/u/tgv)\
**Post date:** [December 1, 2024, 6:26pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/8 "2024-12-01T18:26:56Z")

</div>

I used Cursors for that. Fairly clean, hopefully idiomatic, but not as clean as e.g. a Zip() function that combines to vectors into one. I’m an Ada novice, so I wouldn’t know how to write it, though.

---

<div class="post-metadata">

**Author:** ![FredPraca](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/fredpraca/32/192_2.png) [@FredPraca](https://forum.ada-lang.io/u/FredPraca)\
**Post date:** [December 1, 2024, 7:30pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/9 "2024-12-01T19:30:18Z")

</div>

[fredpraca][01][Ada][My solution](https://gogs.chickenkiller.fr/fred/advent-of-code-2024/raw/main/day_1/historian_hysteria.adb)

I didn’t watch the other solutions but I’m quite sure it’s very similar to evyone else solution 🙂  
I haven’t code in Ada for so long that it took time to do it but it finally works. Good to be back again in Ada programming 😃

---

<div class="post-metadata">

**Author:** ![rassoc](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rassoc/32/519_2.png) [@rassoc](https://forum.ada-lang.io/u/rassoc)\
**Post date:** [December 1, 2024, 7:51pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/10 "2024-12-01T19:51:33Z")

</div>

> [@tgv](#):
>
> not as clean as e.g. a Zip() function

Right, coming from other languages, I’m really missing basic `itertools`-like functionality in Ada. Even worse that you can’t (easily?) write such a general library yourself because of the way generics are working.

```ada
function Solve_Part_1 return Natural is
    use Sorting;
    Total : Natural := 0;
begin
    Sort (Left);
    Sort (Right);
    for I in Left.First_Index .. Left.Last_Index loop
        Total := @ + abs (Left (I) - Right (I));
    end loop;
    return Total;
end Solve_Part_1;

```

feels so inelegant compared to

```ada
sum(abs(id1 - id2) for (id1, id2) in zip(left, right))
-- or even
zip(left, right).map!((id1, id2) => abs(id1 - id2)).sum;

```

---

<div class="post-metadata">

**Author:** ![Chris660](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/c/aeb1de/32.png) [@Chris660](https://forum.ada-lang.io/u/Chris660)\
**Post date:** [December 1, 2024, 9:39pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/11 "2024-12-01T21:39:32Z")

</div>

[My solution](https://github.com/Chris660/AOC-2024/blob/9ab45b8a3a11b5a0bd8e4bacb0545522cbdc6b01/Day_01/main.adb).

Initially I coded it using constrained arrays (knowing the input size), then switched to over Containers.Vectors. In part 2 I tried to take advantage of the fact that the inputs were already sorted, processing the inputs in a single pass.

I’m pretty new to Ada, so spent most of my time of time searching the RM and web rather than writing the code.

---

<div class="post-metadata">

**Author:** ![jere](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jere/32/87_2.png) [@jere](https://forum.ada-lang.io/u/jere)\
**Post date:** [December 2, 2024, 4:50pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/12 "2024-12-02T16:50:51Z")

</div>

> [@](#):
>
> ```ada
> sum(abs(id1 - id2) for (id1, id2) in zip(left, right))
> -- or even
> zip(left, right).map!((id1, id2) => abs(id1 - id2)).sum;
> 
> ```

Wouldn’t this fail if left or right isn’t sorted? Or does `zip()` also presort the inputs? I’m unfamiliar with zip(), so was curious how it worked.

EDIT: One additional way is to use the `Reduce` attribute:

```Ada
Sort (Left);
Sort (Right);
Sum := [for Index in 1 .. Left.Last_Index => abs(Left(Index)-Right(Index))]
         'Reduce("+",0);

```

I put the `'Reduce` on the following line to highlight it. It normally goes on the same line, but up to the programmer of course.

---

<div class="post-metadata">

**Author:** ![rassoc](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rassoc/32/519_2.png) [@rassoc](https://forum.ada-lang.io/u/rassoc)\
**Post date:** [December 3, 2024, 12:36pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/13 "2024-12-03T12:36:55Z")

</div>

Yes, the two examples are incomplete and would fail when executed like that. The actual code is sorting the lists, just like in Ada.

`zip` takes `n` iterators or iterable objects as input and returns a new iterator that continuously yields tuples of `n` elements. And it does that till one of the original iterators runs dry. [Here’s julia’s version, for example](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.zip). Some languages also have the inverse `unzip` function that transforms the resulting single iterator back into multiple lists/iterators again.

Good point regarding `'Reduce` and I think I tried that, but got some weird “left something needs to have a name” error and simply gave up since it was already way too late at my end. 🙂

---

<div class="post-metadata">

**Author:** ![tgv](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/tgv/32/548_2.png) [@tgv](https://forum.ada-lang.io/u/tgv)\
**Post date:** [December 3, 2024, 4:10pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/14 "2024-12-03T16:10:09Z")

</div>

I hadn’t come across 'Reduce yet. Nice!

---

<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 9, 2024, 3:40pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/15 "2024-12-09T15:40:52Z")

</div>

> [@rassoc](#):
>
> Right, coming from other languages, I’m really missing basic `itertools`-like functionality in Ada. Even worse that you can’t (easily?) write such a general library yourself because of the way generics are working.

I’m not sure if it has zip functionality, and I haven’t tested it much myself (if at all – can’t recall), but [Alejandro Mosteo’s iterators library](https://github.com/mosteo/iterators) (available in Alire) looks as if it has at least some of what you’re looking for.

---

<div class="post-metadata">

**Author:** ![rassoc](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/rassoc/32/519_2.png) [@rassoc](https://forum.ada-lang.io/u/rassoc)\
**Post date:** [December 9, 2024, 5:09pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/16 "2024-12-09T17:09:38Z")

</div>

> [@cantanima](#):
>
> […] but [Alejandro Mosteo’s iterators library](https://github.com/mosteo/iterators) (available in Alire) looks as if it has at least some of what you’re looking for.

Thanks for the link. While it’s impressive what he managed to build despite Ada’s rigid generics, you end up with code like this, straight from the readme:

```ada
 package Ints is new Iterators.From.Elements (Integer);
 package Strs is new Iterators.From.Elements (String);
 package Int2Str is new Iterators.Operators (Ints.Iterators, 
                                             Strs.Iterators);

 use Ints.Linking, Strs.Linking, Int2Str.Linking; -- Make "&" visible

 Result : constant Ints.Iterators.List :=
            Ints.Iter (Some_Collection_Of_Integers)
            & Ints.Op.Filter (Some_Condition'Access)
            & Int2Str.Op.Map (Some_Integer_String_Transform'Access)
            & Strs.Op.Collect;

```

opposed to:

```ada
fn main() {
    let result: Vec<String> = [-3_i8, 0, 1, 3].iter()
        .filter(|x| x.is_positive())
        .map(|x| x.to_string())
        .collect();
}

```

And you have to introduce more and more line noise and overhead for every new type or transformation. Not even considering your own data structures aside from `Ada.Containers`.

AdaCore and the ARG are currently cooking and I’m looking forward to anonymous subprograms and the generic improvements proposed in the various AIs.

---

<div class="post-metadata">

**Author:** ![tgv](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/tgv/32/548_2.png) [@tgv](https://forum.ada-lang.io/u/tgv)\
**Post date:** [December 9, 2024, 7:19pm UTC](https://forum.ada-lang.io/t/2024-day-1-historian-hysteria/1523/18 "2024-12-09T19:19:04Z")

</div>

Nice. Thanks for the link. I’m curious to see how it was done. I stumbled getting all the types and functions that make up an iterator aligned, and it’s hard to find concrete examples. Well, I’m a noob anyway.
