r/adventofcode • • Dec 02 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 2 Solutions -🎄-

--- Day 2: Inventory Management System ---


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

Card Prompt: Day 2

Transcript:

The best way to do Advent of Code is ___.


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!

51 Upvotes

407 comments sorted by

View all comments

1

u/DoodleFungus Dec 02 '18

Leaderboard attempt, so bit hacky

input = `pbpaste`.split("\n")

# PART 1
def letcounts(str)
    chars = str.chars
    h = Hash.new 0
    chars.each do |c|
        h[c]+=1
    end
    h
end

counts = input.map { |x| letcounts x }

p counts
twos = counts.count { |h| h.values.any? { |v| v == 2 } }
threes = counts.count { |h| h.values.any? { |v| v == 3 } }

p twos * threes

# PART 2

def comp(a,b)
    a.chars.zip(b.chars).count { |x| x[0] != x[1] } == 1
end

input.each do |i|
    input.each do |j|
        if comp i,j
            p i
            p j
            1/0
        end
    end
end