Skip to content
advent of code 2021

day 19

beacon scanner

approach

scanner 0 is fixed as the reference frame. the solution then repeatedly looks for an unplaced scanner that lines up with any placed one: for each of the 24 possible orientations, it computes the difference between every pair of beacons from the two scanners and counts them. if one offset shows up at least 12 times, that offset is the scanner's position, and its beacons are shifted into the shared frame.

part 1 counts the distinct beacons once every scanner is placed. part 2 takes the largest manhattan distance between any two scanner positions.

the trick

if two scanners really overlap, the same translation shows up for 12 or more beacon pairs, so counting the differences finds both the match and the offset.

python
def overlaps(l1, l2):
    c = Counter(p1.minus(p2) for p1, p2 in product(l1, l2))
    for k, v in c.items():
        if v >= 12:
            return (k, [p.add(k) for p in l2])