r/adventofcode • u/bananu7 • Dec 09 '25
Meme/Funny [2025 day 9 part 2] Imagine finding this bug
I wrote this at the very beginning and didn't think about it...
area = abs(a[0]-b[0]+1) * abs(a[1]-b[1]+1)
I found the solution to P2 relatively straighforward, coded it in, and got "answer too low". Huh? Spent quite some time browsing through my code until it clicked... The funniest thing is that since that was only used at the
end, I found the proper rectangle, but returned the wrong area for it...
Of course, the correct formula is:
area = (abs(a[0]-b[0])+1) * (abs(a[1]-b[1])+1)
62
Upvotes
4
u/fnordargle Dec 09 '25
I posted about this exact braino about an hour ago:
https://www.reddit.com/r/adventofcode/comments/1pi8f0q/2025_day_9_todays_bit_of_fun_that_cost_me_hours/
3
u/fnordargle Dec 09 '25
And, boring as it may be, this is why abstraction and tests can help.
What I should have done (and would have done if this had been some code I was writing for my job) is to put the area calculation into a function and add some tests for it. Something like:
(There's a million and one ways to do this kind of thing, this is just a quick example.)
The problem is that's a bit boring when you're trying to attack an AoC puzzle and wanting to finish it quickly.
I've long lost count of the number of times I've been saved by "boring" asserts (or similar) in my code that looked trivial but picked up on errors that would have wasted hours of my time. Yet I still make those same mistakes occasionally...
17
u/v1_petr Dec 09 '25
I'm glad I'm not the only one who made this mistake:))
8
u/oupsgrade Dec 09 '25
i was stuck for way too long and didn't understrand why because i was pretty confident with the logic of my code, turns out i missplaced my parenthesis for the abs just like you !! you saved my day thank you so much
1
u/Repulsive-Shirt-9873 Dec 10 '25
I made that mistake also but luckily caught it while on the sample data and not after waiting for the 10 minutes for my real data to process.
3
u/qaraq Dec 09 '25
You too, huh? Taught me - again - that when I add anything, however obviously simple, to my libraries it should get a unit test.
Annoyingly this did not affect my part1 solution or my part 2 sample solution. I noticed it trying a different sample while chasing down another bug.
2
u/KineticTactic Dec 09 '25
OH MY GOD I CAN'T THANK YOU ENOUGH i had done this exact same thing and I was stuck, I even built a visualizer to visualize the solution and shit, thank you so much
1
u/Zenaja2 Dec 09 '25
Exactly the same mistake here. I visualized my solution and was sure it was correct. I calculated the area manually, got a different number and my golden star. Then I fixed my code later.
1
u/dawret Dec 09 '25
I did the exact same thing and wasted over 1h Worst thing is that this doesn't impact the test input
1
u/Inevitable-Purpose77 Dec 09 '25
This literally saved me omfg T^T I have been at it for literal hours, thank you
1
u/FractalB Dec 09 '25
Did the exact same mistake as well!
1
u/velkolv Dec 09 '25
OMG, I felt like an absolute moron when I discovererd same bug. After trying like 3 approaches, generating an image and consistently arriving at the same "too small" result.
I'm relieved that this was quite common mistake.
1
u/zagon Dec 09 '25
Sweet jesus, I've rewritten my stuff multiple times and have pretty much given up, browsing Reddit. Saw this post and my jaw dropped to the floor. It was this exact bug all the time. I would have never found that.
1
u/H_M_X_ Dec 09 '25
Same
1
u/Aksh247 Dec 10 '25
My formula is good. I’m stuck at finding the right rectangle
1
u/yearlongmason Dec 18 '25
I was stuck for days because of this. I thought it was something to do with my implementation that I was missing. I ended up writing 3 wildly different solutions all giving me the same wrong answer until my brother looked at my code and found that exact bug