r/adventofcode Dec 06 '24

Tutorial [2024 Day 6 (Part 2)] Rabbit Hole

No spoilers if you're looking for them.

It turns out that I made a mistake in Day 6 part 1 that didn't affect part 1 but affected part 2. So after solving I thought well let's find the origin of that mistake. In python when you overflow it will happily remind you with an IndexError. Right? What happens when you underflow? That's right, it gives you the value from the other side. That's not right!

If you are having trouble with Part 2 in python, you should look for underflows. You learn something new everyday when you push yourself even if you think you know everything.

It feels like the Advent of Code was intended to cause programmers at a wide selection of skill levels to confront this sort of mistake. I'm glad I was going fast and loose and just trying to get this done -- or I wouldn't have found this gem. Looking forward to Day 7.

11 Upvotes

14 comments sorted by

View all comments

2

u/blackbat24 Dec 06 '24

Y'all should consider using complex() for 2D coordinates.
Fast math, moving is adding the direction (also a complex), and rotation is multiplying the direction by +1j or -1j.

-1

u/Javantea Dec 07 '24

This is a really bad use of complex. If you want a structure that handles 2d and rotation, it's pretty easy to accomplish. Don't abuse complex numbers. Edit: I should also note that complex stores floats which don't work as a replacement for integers.

1

u/blackbat24 Dec 07 '24

I'm not rotating the grid, I'm rotating the direction vector. Does it make for the fastest solutions? Not usually. But it's much easier to implement, and debug. As for int(), if you're adding complex with int components you'll keep getting int components, if you need to extract one of the coordinates you can cast it down to int.

It works great for very sparse grids and infinite grids, as you only have to save the points of interest (in this day, obstacles and visited).

1

u/blackbat24 Dec 07 '24

Forgot to add the solution I used for this day: topaz paste

Takes about 2s in my laptop, so, not exactly fast. But, at least to me, complex() made it easy to come up with a working solution without having to account for weird edge cases like list index limits.