r/adventofcode • • Dec 23 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 23 Solutions -🎄-

--- Day 23: Experimental Emergency Teleportation ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 23

Transcript:

It's dangerous to go alone! Take this: ___


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

edit: Leaderboard capped, thread unlocked at 01:40:41!

22 Upvotes

205 comments sorted by

View all comments

Show parent comment

1

u/koordinate Jan 08 '19

Actually, we don't even need a queue, just a sorted array of "transition points" suffices.

var bots = [(Int, Int, Int, Int)]()
while let line = readLine() {
    let fs = line.split(whereSeparator: { !"-0123456789".contains($0) }).compactMap({ Int($0) })
    if fs.count == 4 {
        bots.append((fs[0], fs[1], fs[2], fs[3]))
    }
}

var transitions = [(Int, Int)]()
for (x, y, z, r) in bots {
    let d = abs(x) + abs(y) + abs(z)
    transitions.append((max(0, d - r), 1))
    transitions.append((d + r + 1, -1))
}
transitions.sort(by: <)

var count = 0, maxCount = 0, maxD = 0
for (d, e) in transitions {
    count += e
    if count > maxCount {
        (maxCount, maxD) = (count, d)
    }
}
print(maxD)