Juha-Matti Santala
Community Builder. Dreamer. Adventurer.

Advent of Code tips for beginners - through an example

Advent of Code is right around the corner for the 12th year and it’s time to talk about it again. In short, Advent of Code is an annual Christmas calendar of daily programming puzzles. Each day, you get a puzzle and an input: to solve, you need to write code that solves the problem and submit a numeric result to the website to check if you got it right. Once you solve the first puzzle, a second one is revealed.

It’s a lot of fun and one of my favourite annual things. I solve these puzzles with Python and write explanations that aim to teach people programming and puzzle solving. Since I publish my explanations in my digital garden, I have a separate RSS feed for them.

Warning: this blog post contains spoilers for Advent of Code 2025, day 3 .

Last year, I helped a friend figure out the last few issues with their Advent of Code solution and while we went through it, I made note of a few things that I’ve seen cause difficulties in figuring out what’s wrong and I wanted to share them here.

Here’s the starter code they shared with me. It has the right idea but has a couple of bugs that are leading to incorrect output and causing frustration.

This example is written in Javascript and is meant to be run in the developer console on the AoC input page.

sum = 0; num = 0; id = 0;

data = document.querySelector('pre').innerText.split('\n')

for (i in data) {
  for (j = 0; j < data[i].length; j++) {
    if (data[i][j] > num) {
      num = data[i][j];
      id = j;
    }
  }
  
  num2 = num;
  
  for (; id < data[i].length; id++) {
    if (data[i][j] > num2) {
      num2 = data[i][j];
    }
  }
  
  sum += parseInt(num2) * 10 + parseInt(num);
}

console.log(sum)

Here are a couple of observations I did throughout the process. None of it is Javascript specific and this is not a code review or anything like that. Also, I’m not sharing all the fixes and full working solution here because that’s not the point. It’s just a starting point to make some of my tips more tangible.

Start with example data

One of my first tips is to always start with the provided example data. For this puzzle, Advent of Code provided an example data of four lines:

987654321111111
811111111111119
234234234234278
818181911112111

The examples are really good: they often cover the basic corner cases (but not all!) and are smaller and thus more manageable versions of the main input.

My rule of thumb is to never try with the main output until I get the right answer with the example.

At some point, you need to move from examples to the real input to see if your solution figured out all corner cases. If it then fails, the next good step is to figure out a use case where it fails and create a test input of your own.

There are a couple of benefits with this:

  1. Small example runs faster so your feedback loop becomes shorter. Running the full thing, submitting the result and letting the website tell you if it was right or not is slower and after too many attempts, the website will enforce increasing timeouts.
  2. With small example, you can print out information and be it more understandable and reasonable. Almost always the full input is so big that even intermediary values are difficult to calculate in your head to notice where mistakes happen.
data = [
	'987654321111111',
	'811111111111119',
	'234234234234278',
	'818181911112111'
];

Split the problem (and solution) to smaller chunks

In this example, one challenge with debugging it comes from the fact that it runs the entire input and calculates everything at once.

I like to split the solution into smaller functions that I can then test with inputs where I know the input and output.

For example, here I would start with restructuring the code (but not changing it!) to:

sum = 0; num = 0; id = 0;

function calculateJoltage(bank) {
	for (j = 0; j < bank.length; j++) {
    if (bank[j] > num) {
      num = bank[j];
      id = j;
    }
  }
  
  num2 = num;
  
  for (; id < bank.length; id++) {
    if (bank[j] > num2) {
      num2 = bank[j];
    }
  }
  
	return parseInt(num2) * 10 + parseInt(num);
}

data = document.querySelector('pre').innerText.split('\n')

for (i in data) {
	 sum += calculateJoltage(data[i]) 
}

console.log(sum)

It does exactly the same things but I’ve taken out everything from within the loop into a function.

Now, I can call calculateJoltage function with any individual line I want and compare it to the provided example outputs.

It can be difficult to notice where the bugs are when you get the sum of 4 lines but if you run each line separately, it becomes easier.

Once you get confident that your individual calls to the function are working as intended, you can run the entire thing through with the example input. If it gives the right answer, you can move to the real input and if not, go back to running with individual values until you find where the issue lies.

Print out individual values and do it a lot

First input where I noticed the code failing was 811111111111119 where it returned 99 instead of 89. Running the function with that gives you an idea that the something’s going wrong.

I would put plenty of console.log calls here to see where individual values are.

We have a couple of moving pieces: num, num2, id andj. So I could do something like:

function calculateJoltage(bank) {
	for (j = 0; j < bank.length; j++) {
    if (bank[j] > num) {
      num = bank[j];
      id = j;
    }
  }
  
  console.log({num: num, id: id})
  
  num2 = num;
  
  for (; id < bank.length; id++) {
    if (bank[j] > num2) {
      num2 = bank[j];
    }
  }
  
  console.log({num2: num2})
	return parseInt(num2) * 10 + parseInt(num);
}

and I would notice that the first loop looked at all the numbers and picked the 9 from the end. To satisfy the problem, we need the first number to be the largest of the other numbers but the last one, in this case 8.

We could fix this by adding -1 after bank.length in the first loop condition and our console.log would let us know we found the right number.

After this, we would notice that now we’re getting 98 instead of 89 as the result and see that in our return clause, the numbers are in the opposite order.

Additionally, whenever there’s a calculation at the end, I like to pull that out into a variable so I can inspect it easier. In this case, it would be:

	joltage = parseInt(num) * 10 + parseInt(num2); // with num <-> num2 swap fix implemented
	console.log({joltage: joltage})
	return joltage;

I might refactor that out at the end once everything works but for debugging, it’s really handy.

Sidenote: I like to console.log objects instead of plain values

Let’s compare these two:

let joltage = 99;
console.log(joltage)
// versus
console.log({joltage: joltage})

The first will print “99” into the console. It can be fine if that’s the only thing you print but often when debugging, I’m printing multiple values at different spots and it can be hard to keep track of which is which in my head.

The second will print “{ joltage: 99}” and in the developer console, it’ll even format it into an interactive object.

It makes it also easier to CTRL+F search for the output.

A bonus tip: you can use console.log({joltage}) and Javascript will create an object where there’s key “joltage” with value of the variable “joltage”. It’s a nice shorthand when you want the key to be the same as the variable name.

Advent of Code requires patience, persistence and a ton of iterations

Even great, experienced developers have to go through multiple iterations when testing out their solutions. Being patient and meticulous with your debugging following the tips above helps a lot.

My general advice for debugging is always: don’t try to read the code and figure out what’s wrong. It’s really really hard and keeping track of state of multiple variables and how they change throughout the process. Instead, ask yourself: how do I find out where it’s going wrong?

Start with as small example as possible, print values liberally and often and build your solution piece by piece.

Advent of Code puzzle solving is also a subset of software development that requires practice in puzzle solving to become good at. Most of the real life development is very different and while there are overlapping skills, the overlap is not very large usually.

And if you read others’ solutions and explanations (like mine), remember that you don’t see all the wrong solutions and dozens of iterations but rather the successful, sometimes polished output. Never compare your working progress with someone else’s finished product.

I have 304 stars in Advent of Code over these 11 years. It means I’ve seen a lot of these puzzle types and I have built an intuition where I can connect a problem setting with a possible solution or approach earlier than someone who’s solving these for the first time. It doesn’t make me a better developer than them, just more experienced with these specific puzzles.


If something above resonated with you, let's start a discussion about it! Email me at juhis@hamatti.org and share your thoughts . This year, I want to have more deeper discussions with people from around the world and I'd love if you'd be part of that.