2022 Day 5: Supply Stacks

Not super proud of my solution with this one… An indefinite vector of of vector elements. Got caught up by the ordering of things and confused myself with Append vs Prepend.

@zertovitch’s observed that it’s good to inspect the input first. I did that, and concluded that I wanted an array of arrays, as it’s easy to determine the maximum number of crates, and it isn’t very large. I also noticed that the input is formatted very regularly, so I skipped through the strings to find the correct locations. Naturally, I counted wrong the first 3-4 tries…

a little bit ugly with those Unbounded_Strings… day05.adb

First tried to do Vectors of Vectors, but updating the elements seemed too unnecessarily complex, so I reduced it to Vector of String, since a Vector of Characters is just a String, isn’t it?

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

Note:

  • NOT using any of the Ada.Containers i.e moves of crates are processed while reading the data file – no recording/storage :sunglasses:.
  • Could have best used Ada.Text_IO.Text_Streams and Ada.Streams to read move records of second part of data file. (see package Cranes_IO)

puzzle_05

I ended up going the vector-of-vector route with one being a “vector” and the other acting like
a “stack” (today I learned that Ada doesn’t have a stack library, by design, but thankfully making a “stack” is easy)

link