MENU

Advent of Code Day 3

Went with React today: https://github.com/ChrisBurgdorff/advent-2025-day-3

Part 2 reminded me what these challenges always remind me on their part 2’s and that is choosing the right level of abstraction can save future you extra work.

Part 1 is to choose 2 batteries to turn on. The first battery’s number becomes the tens digit of your “joltage” and the second battery becomes the ones digit.

Undoubtedly, you will write something like this:

const getMaximumJoltage = (bank: string) => {
  let maxTensVal = 0;
  let maxTensIndex = 0;
  let maxOnesVal = 0;
  for (let i = 0; i < bank.length - 1; i++) {
    if (Number(bank[i]) > maxTensVal) {
      maxTensIndex = i;
      maxTensVal = Number(bank[i]);
    }
  }
  for (let j = maxTensIndex + 1; j < bank.length; j++) {
    if (Number(bank[j]) > maxOnesVal) {
      maxOnesVal = Number(bank[j]);
    }
  }
  return (maxTensVal * 10) + maxOnesVal;
};

Notice how the ones and tens shit is hardcoded. Part 2 is to do the same, but choose 12 batteries to turn on.

Now, this is super unrealistic because we all know that real clients and PM’s never change their minds. Humor me though. If you had written part 1 as:

const getMaximumJoltage = (bank: string, numberOfBatteries: number) => {
...

You would be done. If not, you either need to edit the first function with 20 extra variables and loops, or write the abstraction that you should have written in the first place..

Leave a Reply

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