2025 Day 6: Trash Compactor

I found this one unexpectedly fun! Basically parse, then re-parse; if using vectors to store the operands, then reusing the solution to Part 1 is straightforward. Of course, I didn’t do that when doing Part 1; I used an array of 4 elements, but it wasn’t too much trouble to re-work it.

To re-parse the input in Part 2, I transposed it, then used Integer_IO.Get() to re-read the numbers, just as I had for Part 1.

I also made use of Ada 2022’s 'Reduce attribute, but encountered a possible bug when combining it with a vector in a case expression. I’ll look into it more carefully before I report it.

To be honest I am still not done. And I have started to develop a hatred against Adas build in string paring and filtering options and tools. The implementation for the actual calculation is trivial see:

   type Operation is (Mult, Add);        
   Grid    : array (1 .. 4, 1 .. 3) of Integer;
   Operate : array (1 .. 4) of Operation;

   procedure Calc_Result is
      Op    : Operation;
      Colum : Integer;
   begin
      for x in 1 .. 4 loop
         Op := Operate (x);
         Colum := 0;
         for y in 1 .. 4 - 1 loop
            case Op is
               when Mult => Colum := Colum * Grid (x, y);
               when Add  => Colum := Colum + Grid (x, y);
            end case;
         end loop;
      Result := Result + Colum;
      end loop;
   end Calc_Result;

But I can not for the life of me figure out a satisfying way to parse in the data… The methods I use are ugly as hell, use magic numbers everywhere and are hard to understand and debug.

At first I thought “great! I can just start at the (mod 4)th position and get the beginning of each number that way”. But soon I realized that won’t be possible since the start and end positions are not constant…

There is a regex crate that might have helped but I am far too tired and burned out to try it out. (Might be an interesting idea to implement a regex engine in Ada/Spark tho)

I am really exhausted to do these task and I am not sure if I can keep my goal to do all 12 days :frowning:

Yep, that makes this problem a little more challenging.

If you’re stuck parsing Part 1, have you tried something like the following?

--  put each line in its appropriate place
with Ada.Text_IO;

package IO renames Ada.Text_IO;
--  if not using Ada 2022, you may want to with Ada.Text_IO instead
--  and ignore all the IO.'s
package Natural_IO is new IO.Integer_IO (Num => Natural);

declare
   Line: constant String := Ada.Text_IO.Get_Line (Input);
   Position: Positive := Line'First;
   Value: Natural;
begin
   while Position <= Line'Last loop
      Natural_IO.Get (Line (Position .. Line'Last), Value, Position);
      Position := @ + 1; -- if not using Ada 2022, replace @ with Position
      --  then store Value in whatever fashion you find suitable
   end loop;
end;

If Part 2, try the hint I gave in the OP.

I definitely recognize your frustration. If you stick with it, though, you should start to become more comfortable with the Ada language & its standard library. I started mucking around with Ada in 2018 & have never regretted it… well, maybe a couple of times when I was doing my first Advent of Code, back in 2019 or 2020 IIRC.

1 Like

I used Ada.Strings.Fixed.Find_Token` with To_Set (“1234567890-”) and To_Set (“+*”).

1 Like

To follow up: I’ve been trying to translate my Ada solutions into C# on account of having to take on a new project at work, and honestly, I’ve found it to be so much easier in Ada. Maybe it’s because I’m still working my way into the C# mindset, but that can’t be all of it; I think it was yesterday when Enumerable.Range(a, b) overshot b in several different places. I just gave up and changed the foreach loops to the ugly, C-style for loops.

The operators line is spaced so that the operator occurs in the first column of each number, and then the spaces after it account for the size of that column, plus one more space for a separator.

1 Like

I finally managed to do part 1. It is the most heinous code I have ever written. And after reading part 2 I gave up.