AoC 2025, Day 6 - Trash Compactor

Sunday, December 14, 2025

Hoo boy. This one was a goddamn mess for a bit. Here's Day 6: Trash Compactor. Again, spoilers are below for this puzzle. Don't read it if you want to solve this yourself.

Puzzle Summary

After helping in the kitchen, you decide to make things extremely hard for yourself and jump down the trash chute into a trash compactor. I have no idea why, but regardless, a family of cephalopods found you and said they could help you get out. While waiting for the compactor door to open, you volunteer to help with the baby cephalopod's math homework.

The input the cephalopod provided was a list of numbers and the type of math to apply to it:

123 328  51 64 
 45 64  387 23 
  6 98  215 314
*   +   *   +  

Each column contains all the numbers involved in the problem, and the operation at the bottom of the column is the type of math to apply to said numbers.

Part 1 is to summarize the answers of each column.

Part 2 is to use the spacing to make numbers from the columns within the columns and summarize those.

General Strategy

Initially, I just grabbed the puzzle input and only used the numbers and operators in the resulting JSON file:

data6 = [
  ["123", "328", "51", "64"],
  ["45", "64", "387", "23"],
  ["6", "98", "215", "314"],
  ["*", "+", "*", "+"],
];

For part 1, this was fine, but part 2 was more particular. It requires the use of the spacing between numbers. As such, it requires a different style of reading the data.

The puzzle input was instead created as single line string rows:

data6 = [
  "123 328  51 64 ",
  " 45 64  387 23 ",
  "  6 98  215 314",
  "*   +   *   +  ",
];

When we process the data, we read a single column of data at a time. For example, the first read would be 1, , , and *. Operators get processed the same each time, but the numbers will change depending on which part of the puzzle we're doing. If an entire column is nothing but whitespace, all the data for that set has been read, and we begin the next one:

let problemSets = [],
  setCounter = 0,
  total1 = 0,
  total2 = 0;

while (!d.every((i) => i == "")) {
  problemSets[setCounter] = problemSets[setCounter] || {
    part1: [],
    part2: [],
    operator: "+",
  };
  let tmpBuffer = [];
  for (let j = 0; j < d.length; j++) {
    tmpBuffer.push(d[j].substring(0, 1));
    d[j] = d[j].substring(1);
  }
  if (tmpBuffer.every((k) => k == " ")) {
    setCounter++;
  } else {

Thankfully, because we only have multiplication and addition to worry about, the order in which these numbers are processed doesn't matter. We read what operator it is and set it for the problem set:

if (tmpBuffer[tmpBuffer.length - 1] != " ") {
  problemSets[setCounter]["operator"] = tmpBuffer[tmpBuffer.length - 1];
}

Part 1 Solution

Part 1 assumes standard horizontal numbers, so each column row is a different number. We build each number in the problem one digit at a time:

for (let l = 0; l < tmpBuffer.length - 1; l++) {
  problemSets[setCounter]["part1"][l] =
    `${problemSets[setCounter]["part1"][l] || ""}${tmpBuffer[l]}`;
}

Once all problems are cycled through, summarize all part1 sets:

problemSets.forEach((m) => {
  total1 += m.part1.reduce(
    (acc, cur) =>
      m.operator == "*" ? acc * parseInt(cur) : acc + parseInt(cur),
    m.operator == "*" ? 1 : 0,
  );

Part 1 done.

Part 2 Solution

Part 2 assumes vertical numbers, so the column values are joined together as a single number and put into its problem set:

problemSets[setCounter]["part2"].unshift(tmpBuffer.join("").slice(0, -1));

Like part 1, you reduce the part2 sets into total2. Boom, part 2 done.

Final Thoughts and Code

These puzzles require you to read literally every part of the puzzle and take everything at its word. Another great one.

My final code:

var data6 = require("./day06.json");

const Day6B = (d) => {
  let problemSets = [],
    setCounter = 0,
    total1 = 0,
    total2 = 0;

  while (!d.every((i) => i == "")) {
    problemSets[setCounter] = problemSets[setCounter] || {
      part1: [],
      part2: [],
      operator: "+",
    };
    let tmpBuffer = [];
    for (let j = 0; j < d.length; j++) {
      tmpBuffer.push(d[j].substring(0, 1));
      d[j] = d[j].substring(1);
    }
    if (tmpBuffer.every((k) => k == " ")) {
      setCounter++;
    } else {
      for (let l = 0; l < tmpBuffer.length - 1; l++) {
        problemSets[setCounter]["part1"][l] =
          `${problemSets[setCounter]["part1"][l] || ""}${tmpBuffer[l]}`;
      }
      problemSets[setCounter]["part2"].unshift(tmpBuffer.join("").slice(0, -1));
      if (tmpBuffer[tmpBuffer.length - 1] != " ") {
        problemSets[setCounter]["operator"] = tmpBuffer[tmpBuffer.length - 1];
      }
    }
  }
  problemSets.forEach((m) => {
    total1 += m.part1.reduce(
      (acc, cur) =>
        m.operator == "*" ? acc * parseInt(cur) : acc + parseInt(cur),
      m.operator == "*" ? 1 : 0,
    );
    total2 += m.part2.reduce(
      (acc, cur) =>
        m.operator == "*" ? acc * parseInt(cur) : acc + parseInt(cur),
      m.operator == "*" ? 1 : 0,
    );
  });
  return `${total1} - ${total2}`;
};

console.log(Day6B(data6));