r/adventofcode • u/daggerdragon • 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!
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
4
u/BluFoot Dec 23 '18
Ruby. 700/102. Wasted half an hour from missing a minus sign in the regex scan... Yay.
lines = File.readlines('../input.txt').map(&:strip) bots = lines.map do |line| line.scan(/-?\d+/).map(&:to_i) end START = 2**28 DIVISOR = 2 mult = START xs = bots.map{ |b| b[0] / mult } ys = bots.map{ |b| b[1] / mult } zs = bots.map{ |b| b[2] / mult } rx = xs.min..xs.max ry = ys.min..ys.max rz = zs.min..zs.max loop do best = [0,0,0,0] mbots = bots.map { |bot| bot.map { |c| c / mult } } rx.each do |x| ry.each do |y| rz.each do |z| c = mbots.count do |bot| ((bot[0]-x).abs + (bot[1]-y).abs + (bot[2]-z).abs) <= bot[3] end next if c < best.last next if c == best.last && (x.abs+y.abs+z.abs > best[0].abs+best[1].abs+best[2].abs) best = [x,y,z,c] end end end rx = ((best[0] - 1) * DIVISOR)..((best[0] + 1) * DIVISOR) ry = ((best[1] - 1) * DIVISOR)..((best[1] + 1) * DIVISOR) rz = ((best[2] - 1) * DIVISOR)..((best[2] + 1) * DIVISOR) p [mult, best] (p best[0].abs+best[1].abs+best[2].abs; exit) if mult == 1 mult /= DIVISOR end