A big orange industrial garbage compressor

Day 6: Trash Compactor

I found part 2 of this puzzle rather fiddly. Difficulty is definitely increasing, bit by bit.

Note: If you’re attempting to read this as a Fediverse post, you might find it rather confusing. I recommend using a web browser to read it properly, following this link.

The challenge

Input was this time a list of number tupels, separated by blanks:

12 456 4 56

Each tupel has the same number of elements, so that we get something like a spreadsheet with rows and columns. The last item of the list contained one of the arithmetic operators + or *.

The task was to apply the corresponding arithmetic operation to each column of our spreadsheet and to sum up the results.

The idea

The first step is to store all numbers in a 2-dimensional array and then to apply the corresponding operation to each column.

Perl code Link to actual code (accessible) in caption
https://codeberg.org/M1na/AOC25/src/branch/main/aoc2506a.pl

In case, you’re a Perl savvy person, you might wonder why I don’t use the convenience functions of some module, say, from List::Utils, or any modern keywords, like say.

I try do avoid these, if I can, to make the programs as portable as possible. Perl is installed on every Un*x machine or Mac, but I do not know the version, nor which additional modules might be installed, and yet, I want people to be able to just drop the file into a folder, make it executable with chmod +x program.pl and be able to execute ./program.pl input_file

The updated challenge

Oh no! The columns are not separated by an arbitrary amount of spaces, but by a single one, whilst the numbers are either preceded or followed by blanks to give every element of each column the same length. The orientation of the numbers in any given column doesn’t change.

We’re now supposed to read numbers upwards down in each column and then to apply the corresponding arithmetical operation.

The updated idea

In order to extract the numbers correctly, we need to know the width of each column (they can vary). Fortunately, the last line with the operators is always left oriented and has the same width as the rest of the column. So, we get each column’s with by just counting the white spaces after each operator.

The only exception is the last column. Whilst possibly being left oriented, it has no trailing spaces. We correct this by adding the missing ones.

We now just need to concatenate the digits at the same position in every column and apply the operation.

Perl code Link to actual code (accessible) in caption
https://codeberg.org/M1na/AOC25/src/branch/main/aoc2506b.pl

Afterthought

I should have transposed the input right in the beginning (actually: after all). Would have made things much more straight forward.

Fediverse Reactions

Comments

2 responses to “Day 6: Trash Compactor”

  1. @AdventOfCode

    The only exception is the last column. Whilst possibly being left oriented, it has no trailing spaces.

    My input was perfectly rectangular. Even the last column had space padding on the right.

    1. @barubary

      I checked again.

      Mine had *no* padding on the right!

      @AdventOfCode

Leave a Reply

Your email address will not be published. Required fields are marked *