Skip to main content

Advent of Code Day 9 Troubleshooting

·865 words·5 mins

^ this is my whiteboard after tryna solve day 9.

Okay, I’m stuck.

I’ve spent five hours on the problem for day 9 now. I have an approach which I believe should work and I believe it is implemented correctly but I’m getting the wrong answer.

So one of my beliefs is clearly wrong. Maybe you can rubber duck and help me figure it out.

The approach. I’m tracing the edge tiles by following the pairs of coords from the input. Maybe there’s a sequence of coords that doesn’t turn? As I move along the edge tiles, I add the tiles just outside the edge to a list of bad coords. To do this I have to add the corners separately from the lines. I test for the eight possible corners and depending on whether it’s an inner corner or an outer corner I add one or three bad tiles. Then I add the bad tiles for the lines.

After that, I walk over the edge tiles again, setting these back from bad to good. I realized that when edge tiles are right next to each other, I can have the bad outside tiles write over the border, which isn’t how it should be handled. So walking over the edge tiles and setting them all back to good tiles addresses that.

I guess I’m not addressing the potential case where the polygon overlaps itself. They wouldn’t do that to us, would they? If they did, I should be able to detect it. Figuring out how to solve it is another matter, though.

Once I have a list of bad tiles, I calculate the areas of rectangles created from all possible combinations of corners. Then I sort them in descending order and iterate starting with the largest area rectangles. If the rectangle contains any of the bad points from my list then I move onto the next rectangle. Once I find a rectangle that does not contain any bad points then that’s the solution.

I could probably speed it up by testing the bad corner tiles first. Or maybe even shuffling the list of bad tiles.

Oh wow it’s so much faster. A quick shuffle and it went from taking ~30 seconds to find a solution to a couple seconds. Okay, let’s measure. Before: 57 seconds. After: 2 seconds. Wowee!

That’s cool but unfortunately the answer is still very wrong.

Checking to see if the border crosses itself. It looks like a no.

Oh yeah, I should re-read the problem, too.

Whoa, I found the problem!

I don’t know whether to be happy or sad, haha. I’ve had the correct algorithm and implementation for a while, at least as far as tracking tiles goes.

The problem was in my function for calculating the area of a rectangle! Ouchie it hurts!

The function works fine when the opposite corners are ordered from top left to bottom right and returns a result that is too small in other cases.

Oh man.

Here’s the offending line:

area = abs(p1[0] - p0[0] + 1) * abs(p1[1] - p0[1] + 1)

And here’s the fix:

area = (abs(p1[0] - p0[0]) + 1) * (abs(p1[1] - p0[1]) + 1)

So I’m getting the area by multiplying the width and height but I need to add one to the width and height because in the problem we include all four edges in the count. That part is fine.

If I provide two corners like [2, 3] [6, 8] then it works fine. The deltas are [4, 5] and I add one to each to get [5, 6] and get the correct answer: 30.

But if I have corners [8, 2] [1, 8] where the x-coordinate is going from the high value to the low one then the deltas are [-7, 6] and when I add one I get [-6, 7] and get a smaller computed area of 42. What should happen is I take the absolute of the deltas first, then increment them so [-7, 6] becomes [7, 6] becomes [8, 7] and I get the correct answer, 56.

Oh man. I only found it because I was creating a new test input to ensure that my self-crossing boundary check would detect overlaps correctly. I hadn’t even gotten that far before I somehow noticed the computed area for the current answer was incorrect.

I wonder how long it would’ve taken me to find that otherwise. I’ve already spent a couple hours troubleshooting other things before I realized the mistake had been sitting there all this time.

It’s actually been sitting there since part one, like one hour into this.

Phew! What a ride! Hahaha

These puzzles are such a good experience for me. I’m learning a lot and also getting a great perspective on what I need to work on.

Thanks for the rubber ducking. Full solution here: https://github.com/nielmclaren/AdventOfCode/blob/main/2025/day9/main.py

Corners
#

The most fun part about this problem was figuring out how to handle corners when drawing a border around the outside of the boundary tiles. It’s been a while since I had to do serious whiteboarding to wrap my head around the cases. Fun stuff and I’m pretty happy with the solution.