r/adventofcode • • 1d ago

Past Event Solutions [2021 Day 19] Finally solved beacons after five years :’)

I restarted this for probably the fifth time (but maybe third serious time) last night and stayed up until 4:30AM working on it, drawing lots of pictures. I am not a better programmer at 3:00AM and this is probably the first/most hand written code I’ve done since spring. I was a little worried I’d forget everything I was doing if I went to bed but I woke up three hours later and found the last bug. 🥹 part 2 was really easy except I printed the most recent distance instead of the longest distance and also tried to find the furthest apart beacons instead of scanners. That was easily fixed by reading/trying the sample.

Here is my code

33 Upvotes

8 comments sorted by

4

u/AustinVelonaut 1d ago

but I woke up three hours later and found the last bug.

I remember that problem! That seems to be a common occurrence with me during AoC: I'll go to sleep not having solved something, and my subconscious works on it and hands me the solution in the morning.

3

u/DelightfulCodeWeasel 1d ago

Huge effort, well done! 

Nothing quite as satisfying as bagging your white whale.

2

u/careyi4 1d ago

Well done, that’s a hard problem, I remember it breaking my brain when I did it

2

u/terje_wiig_mathisen 1d ago

Well done for not giving up!

I remember the problem, it was during the final week of an internal corporate leaderboard competition.

I was lucky enough that the first approach I tried (fingerprinting scanners by the set of squared distances between the beacons it had detected) worked. The idea is somewhat similar to the SIFT algorithm used by all automatic panorama stitchers, except simpler because it does not need to be "Scale-Invariant".

Looking at my 5 year old code, I realized that I have not written any Rust versions for this year, so that's something I can look forward to after we finish this year.

My Perl code runs in half a second, it was the first day that year that I needed 2+ hours to find the solution, I was obviously quite happy because the code still contains a bunch of debug printf() statements that I didn't subsequently cleanup!

3

u/DelightfulCodeWeasel 1d ago

If you're doing a Rust revisit, you might be interested in some findings I made in my revisit last month.

The main one is that the inputs may all have the nice property that the pair { Euclidean(a, b), Manhattan(a, b) } is globally unique for all pairs of beacons.

2

u/RazarTuk 1d ago

Extremely tangential: If you're making functions for Euclidean and Manhattan distance, I'd just generalize it to any p-norm, with double positive infinity being the L-infinity norm. (Max absolute value of any component) At least if you use the cube-based method for hex coordinates, where it's (x,y,z) with x+y+z=0, distance is actually use L-infinity

2

u/RazarTuk 1d ago

Actually, more specifically with hex stuff: The easy way to do hex grids is to map all three "axes" to x/y/z, with the constraint that x+y+z must equal 0, although you can also reduce it to 2 axes at a 60° angle because the last one can be computed. At least for a vertical grid, like in 2017 Day 11, the basic directions are N = [0, -1, 1], NW = [-1, 0, 1], NE = [1, -1, 0]. And then one benefit of tracking all three coordinates instead of just two is that, if you have the L-infinity norm implemented, you can just use that for hex distance

There are other coordinate systems for hexes, but this one interacts so nicely with the sort of math you might need to do for AoC, like distance or pathfinding, that it's what I've implemented

1

u/DelightfulCodeWeasel 1d ago

Cube coordinates is exactly what I used on my 2017 day 11 solution as well, all wrapped up in both pointy and flat flavours as needed, although so far it's only been used in one puzzle! Offset coordinates were neater in terms of memory layout for 2020 day 24.