# 2024 Day 5: Print Queue

**URL:** <https://forum.ada-lang.io/t/2024-day-5-print-queue/1550>\
**Category:** Advent of Code\
**Created:** [December 5, 2024, 5:59am UTC](https://forum.ada-lang.io/t/2024-day-5-print-queue/1550 "2024-12-05T05:59:57Z")\
**Posts on this page:** 6\
**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 5, 2024, 5:59am UTC](https://forum.ada-lang.io/t/2024-day-5-print-queue/1550/1 "2024-12-05T05:59:57Z")

</div>

I didn’t think too hard about optimization here, just implemented the problem as written. I’m not really happy with my solution, but it works so I guess that’s good enough.

Might do some cleanup/optimization later.

[advent/2024/src/day5\_2.adb at 15a3610d7b158f0973baa5ccd0a02bbad4a50af5 · JeremyGrosser/advent · GitHub](https://github.com/JeremyGrosser/advent/blob/15a3610d7b158f0973baa5ccd0a02bbad4a50af5/2024/src/day5_2.adb)

---

<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 5, 2024, 6:58am UTC](https://forum.ada-lang.io/t/2024-day-5-print-queue/1550/2 "2024-12-05T06:58:56Z")

</div>

Looking at other people’s solutions on reddit I can see that it’s just a custom sort operator. I implemented a bubble sort from scratch without really thinking about what I was doing.

---

<div class="post-metadata">

**Author:** ![jcmoyer](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/jcmoyer/32/373_2.png) [@jcmoyer](https://forum.ada-lang.io/u/jcmoyer)\
**Post date:** [December 5, 2024, 2:15pm UTC](https://forum.ada-lang.io/t/2024-day-5-print-queue/1550/3 "2024-12-05T14:15:01Z")

</div>

> [@JeremyGrosser](#):
>
> Looking at other people’s solutions on reddit I can see that

Super interesting. I didn’t catch that and instead went full graph theory and implemented topological sort. It also looks like some people ran into issues with topo sort. I did too at first (you can’t find root nodes for a sort if you consider the whole graph), but I solved them by only considering the subgraph containing the page numbers in each list.

[puzzles/AdventOfCode2024/src/day05.adb at 21a7dbbe3c101e1f04b064b793847929209d6faa · jcmoyer/puzzles · GitHub](https://github.com/jcmoyer/puzzles/blob/21a7dbbe3c101e1f04b064b793847929209d6faa/AdventOfCode2024/src/day05.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 5, 2024, 4:36pm UTC](https://forum.ada-lang.io/t/2024-day-5-print-queue/1550/4 "2024-12-05T16:36:32Z")

</div>

I tried the sorting first, then ran into a problem, read your comment, tried it again, and got it to work. Basically this:

```
     procedure Sort_Page_Number_List is new Ada.Containers.Generic_Sort
        (Index_Type => Natural, Before => Page_Number_In_Order,
         Swap => Page_Number_Swap);

```

It’s neat, but the bad thing is: you can’t infer from the problem that this should work. I was almost going to make a transitive closure of the ordering, which I now understand would lead to more trouble.

---

<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 5, 2024, 8:42pm UTC](https://forum.ada-lang.io/t/2024-day-5-print-queue/1550/5 "2024-12-05T20:42:57Z")

</div>

My approach:

As I read in the page orderings, I build a Hashed\_Map of Hashed sets with the left number as the key to the map, and the set containing all right numbers that had it as a left number.

EX:

```ada
[47] => [13, 29, 53, 61],
[97] => [13, 29, 47, 53, 61, 75]
etc.

```

Then when I read in the update orders after, I iterated starting with the 2nd number and verified it either wasn’t a key to the map (no association) or the all of the numbers to the left of it were not in its hashed set if it did exist as a key.

EX:

```Ada
   -- Here the upper loop number is supplied to Left and the numbers to the
   -- left of it are supplied to Right and we verify if there is a specified order
   -- that matches
   function Must_Be_Before(Left, Right : Page_Number; Map : Page_Map) return Boolean is
      Map_Cursor : constant Page_Maps.Cursor := Map.Find(Left);
      Set_Cursor : constant Page_Sets.Cursor := 
         (if Map_Cursor in Page_Maps.No_Element then 
            Page_Sets.No_Element
          else 
            Map(Map_Cursor).Find(Right));
   begin
      return Set_Cursor not in Page_Sets.No_Element; 
   end Must_Be_Before;

```

So for a list of 5 numbers:  
1 check vs the 2nd number  
2 checks vs the 3rd number  
3 checks vs the 4th number  
4 checks vs the 5th number  
etc.

So not dissimilar to some sorting algorithms, but checking as I go without actually sorting the page numbers.

---

<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 9, 2024, 5:24pm UTC](https://forum.ada-lang.io/t/2024-day-5-print-queue/1550/6 "2024-12-09T17:24:29Z")

</div>

Jeez, I keep falling behind! Lots of (much needed) practice using maps and vectors for this one.

[advent-of-code/2024/day\_5\_1/src/day\_5\_1.adb at 9d9604d26cba85e8b4147391cfd7617e6a3d64c5 · alexispaez/advent-of-code · GitHub](https://github.com/alexispaez/advent-of-code/blob/9d9604d26cba85e8b4147391cfd7617e6a3d64c5/2024/day_5_1/src/day_5_1.adb)
