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!

23 Upvotes

205 comments sorted by

View all comments

Show parent comment

2

u/p_tseng Dec 25 '18 edited Dec 25 '18

At this point, I've explored a bunch of solutions and I'm going to throw my hat in with the strategy of splitting the search space into octants and calculating an upper bound of the number of intersections for the regions created. When you contract down to a single point, you know the bound is exact. When all remaining candidates have upper bounds smaller than the best you've found so far, you terminate the search because you know you can do no better than what you've already found.

This approach is described by:

Note that there is a related strategy of "divide an octahedron into six smaller octahedra". I implemented this solution as well but for me these solutions find an initial answer very fast, and then take minutes to disprove all other candidates. I have not fully investigated the reasons for this. For clarity, the strategy is described in:

I haven't investigated why, but nevertheless I am going to stick with the splitting into eight octants. As discussed elsewhere, it is possible to construct inputs that expose its worst-case running time, but for four different Advent of Code inputs it finds the correct solution (with the correct number of bots) in 1 second on my machine.

Ruby:

require_relative 'lib/priority_queue'

def closest_1d(target, low, high)
  return 0 if low <= target && target <= high
  target > high ? target - high : low - target
end

def farthest_1d(target, low, high)
  return [target - low, high - target].max if low <= target && target <= high
  target > high ? target - low : high - target
end

class Box
  attr_reader :min, :max

  # min/max are both [x, y, z], inclusive
  def initialize(min, max)
    @min = min.freeze
    @max = max.freeze
  end

  def translate(deltas)
    self.class.new(@min.zip(deltas).map(&:sum), @max.zip(deltas).map(&:sum))
  end

  def empty?
    @min.zip(@max).any? { |min, max| min > max }
  end

  def point?
    @min.zip(@max).all? { |min, max| min == max }
  end

  def touched_by?(bot)
    *pos, r = bot
    pos.zip(@min, @max).sum { |args| closest_1d(*args) } <= r
  end

  def contained_by?(bot)
    *pos, r = bot
    pos.zip(@min, @max).sum { |args| farthest_1d(*args) } <= r
  end

  def size
    @min.zip(@max).reduce(1) { |acc, (min, max)|
      dim = min > max ? 0 : (max - min + 1)
      acc * dim
    }
  end

  def min_dist
    @min.zip(@max).sum { |min, max| closest_1d(0, min, max) }
  end

  def split
    mid = @min.zip(@max).map { |min, max| (min + max) / 2 }

    8.times.map { |bits|
      newmin = [
        bits & 1 == 0 ? @min[0] : mid[0] + 1,
        bits & 2 == 0 ? @min[1] : mid[1] + 1,
        bits & 4 == 0 ? @min[2] : mid[2] + 1,
      ]
      newmax = [
        bits & 1 == 0 ? mid[0] : @max[0],
        bits & 2 == 0 ? mid[1] : @max[1],
        bits & 4 == 0 ? mid[2] : @max[2],
      ]
      self.class.new(newmin, newmax)
    }.reject(&:empty?)
  end

  def to_s
    @min.zip(@max).map { |min, max| min == max ? min : Range.new(min, max) }.to_s
  end
end

module Nanobot refine Array do
  def dist(pt)
    pt.zip(self).sum { |x, y| (x - y).abs }
  end
end end

surround = ARGV.delete('--surround')
verbose = ARGV.delete('-v')

bots = (ARGV.empty? ? DATA : ARGF).each_line.map { |l|
  l.scan(/-?\d+/).map(&:to_i).freeze
}.freeze

using Nanobot

*best_bot, best_r = bots.max_by(&:last)
puts "best bot @ #{best_bot} w/ radius #{best_r}" if verbose
puts bots.count { |*bot, _| bot.dist(best_bot) <= best_r }

# Part 2:
# Split the search region into octants,
# dividing a large cube into eight smaller ones.
#
# Lower bound: Number of bots that fully contain a region
# Upper bound: Number of bots that touch a region
#
# We explore areas with the best upper bounds.
# When we explore a point (or a region where lower == upper),
# we know the exact number of intersections there.

def coords(bots)
  [0, 1, 2].map { |i| bots.map { |b| b[i] } }
end

# Start w/ something covering all bots.
coords = coords(bots)
box = Box.new(coords.map(&:min), coords.map(&:max))

puts "start w/ #{box}" if verbose

def most_intersected(start, bots, verbose: false)
  dequeues = 0

  pq = PriorityQueue.new
  # We need to order by [max upper bound, min dist]
  # This allows us to terminate when we dequeue a point,
  # since nothing can have a better upper bound nor be closer to the origin.
  #
  # Supposedly, adding size to the ordering speeds things up,
  # but I did not observe any such effect.
  #
  # I was NOT convinced that adding -lower_bound to the ordering improves anything.
  pq[start] = [-bots.size, start.min_dist, start.size]

  while (region, (neg_upper_bound, _) = pq.pop(with_priority: true))
    dequeues += 1
    outer_upper_bound = -neg_upper_bound

    if region.point?
      puts "dequeued #{region} w/ #{outer_upper_bound} bots, after #{dequeues} dequeues" if verbose
      return region
    end

    region.split.each { |split|
      #lower_bound = bots.count { |b| split.contained_by?(b) }
      upper_bound = bots.count { |b| split.touched_by?(b) }
      pq[split.freeze] = [-upper_bound, split.min_dist, split.size]
    }
  end
end

best_region = most_intersected(box, bots, verbose: verbose)
puts best_region.min_dist

best_count = bots.count { |b| best_region.contained_by?(b) }

(-3..3).each { |dx|
  (-3..3).each { |dy|
    (-3..3).each { |dz|
      new_box = best_region.translate([dx, dy, dz])
      count_here = bots.count { |b| new_box.contained_by?(b) }
      puts "#{new_box}: #{count_here}#{' WINNER!' if count_here == best_count}"
    }
  }
} if surround

__END__
pos=<64355281,-4578031,8347328>, r=89681260
pos=<57301484,24998650,93936786>, r=75762903
pos=<96148809,8735822,40200623>, r=76307124