r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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

ATTENTION: minor change request from the mods!

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

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

1

u/rock_neurotiko Dec 03 '18

Day 3 in Ruby:

format_r = /#(.+) @ (\d+),(\d+): (\d+)x(\d+)/

data = Hash.new 0

boxes = File.open("input").map do |line|
  id, left, top, wide, tall = line.match(format_r).captures
  left = left.to_i
  top = top.to_i
  wide = wide.to_i
  tall = tall.to_i

  right = left + wide - 1
  bottom = top + tall - 1

  (left..right).each do |i|
    (top..bottom).each do |j|
      coords = [i, j]
      data[coords] += 1
    end
  end


  [id, left, top, wide, tall]
end

puts "Solution 1:", data.values.select { |x| x > 1 }.count

def collides?(data, left, right, top, bottom)
  for i in (left..right) do
    for j in (top..bottom) do
      coords = [i, j]
      return false if data[coords] > 1
    end
  end

  true
end

one_remain = boxes.select do |box|
  _, left, top, wide, tall = box
  right = left + wide - 1
  bottom = top + tall - 1

  collides?(data, left, right, top, bottom)
end

puts "Solution 2:", one_remain[0][0]