MAX = { red: 12, green: 13, blue: 14 } #prepare the input which is a string containing new lines def parse(input) data = [] input.each_line do |line| #Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green game_num = line.split(":").first.split(" ").last.to_i draws = line.chomp.split(":").last.split(";").map {|e| e.split(",") } h = {} h[game_num] = draws data << h end data end # result should a single string or integer def calculate(data) result = 0 data.each do |d| game_num = d.keys.first draws = d.values.first fits = draws_fits?(draws) result += game_num if fits puts "#{game_num} is #{fits ? "in": "out"}" end result end def draws_fits?(draws) draws.each do |set| set.each do |draw| num, color = draw.strip.split(" ") return false if num.to_i > MAX[color.to_sym] end end true end