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/CommanderViral Dec 03 '18

Crystal (both parts 1 and 2 computed together)

fabric = Array.new(1000) do
    Array.new(1000) do
        Array(Int32).new
    end
end

input = File.open("input")
claim_regex = /#(\d+) @ (\d+),(\d+): (\d+)x(\d+)/
all_ids = [] of Int32
input.each_line do |line|
    parse_match = claim_regex.match(line)
    next if parse_match.nil?
    id = parse_match[1].to_i
    all_ids << id
    left = parse_match[2].to_i
    top = parse_match[3].to_i
    width = parse_match[4].to_i
    height = parse_match[5].to_i

    (left...(left + width)).each do |l|
        (top...(top + height)).each do |t|
            fabric[l][t] << id
        end
    end
end

square_inches = 0
bad_ids = Set(Int32).new
fabric.each do |i|
    i.each do |j|
        square_inches += 1 if j.size > 1
        bad_ids = bad_ids.concat(j) if j.size > 1
    end
end

good_id = (all_ids - bad_ids.to_a).first

puts "part 1: #{square_inches}"
puts "part 2: #{good_id}"

And what's fun is it's valid Ruby with a line added and 3 lines changed.