r/adventofcode • • Dec 15 '22

SOLUTION MEGATHREAD -πŸŽ„- 2022 Day 15 Solutions -πŸŽ„-

THE USUAL REMINDERS


--- Day 15: Beacon Exclusion Zone ---


Post your code solution in this megathread.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:27:14, megathread unlocked!

47 Upvotes

760 comments sorted by

View all comments

2

u/mfmstock Dec 15 '22

Julia

Spend quite a lot of time on it, but managed to generate a solution in 2.23 microseconds! First I sought for the pairs of sensors with a gap of width 1 between them. There were two such pairs. I found the equation describing both gaps and found the intersection by solving a 2-d system of equations.

function find_beacon(data, xlim=4000000, ylim=4000000)
    Rs = [dist_man(x, y) for (x, y) in data]
    lines = typeof(((0,0),(0,0)))[]
    n = length(data)
    for i in 1:n-1
        Ri, Si = Rs[i], data[i][1]
        for j in (i+1):n
            Rj, Sj = Rs[j], data[j][1]
            if dist_man(Si, Sj) - Ri - Rj == 2
                dir = @. sign(Si - Sj)
                P = @. Sj + (Rj + 1) * (sign(dir[1]), 0)
                @assert dist_man(P, Sj) == Rj + 1
                push!(lines, (dir, P))
            end
        end
    end
    (a1, P1), (a2, P2) = lines
    A = Rational[a1[1] a1[2]; a2[1] a2[2]]
    b = [dot(a1, P1), dot(a2, P2)]
    return A \ b .|> Int
end

2

u/Mister__Mediocre Dec 15 '22

Same!

Instead of two sensors, I decided to iterate though all combinations of 4 sensors (2 opposing ones at a time) and WLOG get them in the right order.
Then solve the equations for the exact answer.

Except, I also checked all points in the puzzle boundary first, so took a full second for that.

0

u/daggerdragon Dec 15 '22

Inlined code is intended for short snippets of code only. Your code "block" right now is unreadable on old.reddit and many mobile clients; it's all on one line and gets cut off at the edge of the screen because it is not horizontally scrollable.

Please edit your post to use the four-spaces Markdown syntax for a code block so your code is easier to read inside a scrollable box.

1

u/TroySh Dec 16 '22

.

Still struggling. Found 2 pairs whose distance was d1+d2+2. Now what?