Advent of Code 2025 Day 6: Trash Compactor

Every year, the first weekend of Advent of Code is where Eric feels he can start upping the difficulty. Knowing this, it was with more than a little trepidation that I opened up today’s puzzle this morning.

Math homework

For part 1, we take each column of numbers in turn and use the operator (* = multiplication, + = addition) to reduce the column to a single value. Then all those solutions are reduced with addition to become the part 1 answer.

I use the word reduce on purpose; reduce is a standard Python function (and present in many other languages) that applies a given function to each element of a list in turn, collapsing the list to a single value.

Naturally, Lua doesn’t have this function. And naturally, the Lua community has stepped up.

Operator error! OH NO!

For part 1, though, since I noticed that the only operations were addition and multiplication – and since they are commutative, so they can be performed in any order – I just accumulated the intermediate values in a table. For part 1, I looked at the rows in reverse (again, it’s commutative, so this is fine) so that I could know what operations I needed to perform on the list up front. The first line (going in reverse) was the operators; the second line (the third line in the sample data above) was the seed value; and for that line and all the other lines back to the first, I’d add up all the intermediate values (with reduce) and accumulate that into the part 1 answer.

You can see in the code fragment that I turned the plus signs and asterisks into function pointers to my trivial “add two numbers” and “multiply two numbers” functions, so I could accumulate the values correctly with no further conditionals later on.

I make it sound harder than it is. If I really had wanted to Lua, I would have changed the prototype to redefine the meaning of addition or multiplication to bypass having to call the functions at all. This is something I’ll probably end up doing at some point, but right now it would just be needlessly complex. It’s supposed to be fun.

Cephalopod math?

Let’s check the bold words to see if we can get some hints as to what part 2 will look like. “List of problems”, yup. “Added”, “multiplied”, simple enough. Maybe it’s supposed to be base 8 or something weird? Let’s see.

Oh. Cephalopods are Chinese.

It’s on part 2 that half the world suddenly sits back and goes, “well yeah, this is how it’s supposed to be.” English – and its math – are written left to right, top to bottom. Chinese, Japanese, and many other languages are traditionally written top to bottom, right to left. This is exactly how part 2 works.

If only our input could come like that, we wouldn’t have any problems. And so that’s what I did in part 2. I took the input as a whole, and working backward from the end of each line, accumulated everything in that column into a string. If it was blank, I moved on. Since the final character of this line would be either a space, a +, or a *, and the other characters would be a number, I appended the number to a list of numbers. If the final character was an operator, I then reduced the number list with that operator, accumulated the result into the part 2 total, cleared the list, and continued on.

We essentially rotate the puzzle a quarter turn.

That’s really all there was to it.

I still haven’t reached the point where I just plain can’t figure out how to solve these puzzles in Picotron’s Lua variant, or where the language is too slow. I’m not sure, but I think I’m nearing the point where in previous years I abandoned whatever new thing I planned to do (like using Pico-8, or Java, or Clojure, etc.) and just went back to Python. I’m staying as strong as I can. This is the halfway point. It only gets more fun from here.

Leave a Reply

Discover more from West Karana

Subscribe now to keep reading and get access to the full archive.

Continue reading