Ruby, part 2. Oddly much easier than part 1. Parsing modified to suit the problem better:
require'set'bag_descriptions={}File.readlines('07.txt').eachdo|line|bag_type=line.match('\A(.*?) bags')[1]contents=[]unlessline.match('contain no other bags')line.scan(/(\d+) (.*?) bags?/).eachdo|count,color|contents.push[color,count.to_i]endendbag_descriptions[bag_type]=contents.to_henddeftotal_bag_count(bag_descriptions,bag_type)# Count this bagcount=1bag_descriptions[bag_type].eachdo|inner_bag,bag_count|count+=bag_count*total_bag_count(bag_descriptions,inner_bag)endcountend# Subtract one as the shiny gold bag itself is not countedputstotal_bag_count(bag_descriptions,'shiny gold')-1
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Ruby, part 2. Oddly much easier than part 1. Parsing modified to suit the problem better: