re: AoC Day 2: Inventory Management System VIEW POST

VIEW FULL DISCUSSION
 

Parts 1 and 2 in Ruby:

require 'set'

box_ids = DATA.read.split.map(&:each_char)
counts = Hash.new(0)

box_ids.map do |box_id|
  char_counts = box_id.group_by(&:itself).values.map(&:count)
  Set.new(char_counts).each { |n| counts[n] += 1 }
end

puts counts[2] * counts[3] # 6474

pair = box_ids.combination(2).map { |a1, a2| a1.zip(a2) }.lazy.find { |zipped|
  zipped.count { |c1, c2| c1 != c2 } == 1
}

# mxhwoglxgeauywfkztndcvjqr
puts pair.map { |c1, c2| c1 if c1 == c2 }.join

__END__
myhposlqgeauywfikztndcvrqr
mbhposlxfeauywoikztndcvjqi
mbhpoulxgeagywfikytndcvjqr
jbhposlxgeauywdikztndcvjqk
mbhpsslxueauywfikzfndcvjqr
mbhposnxgeauzyfikztndcvjqr
# data section abbreviated
code of conduct - report abuse