CONSTRAINTS = { red: 12, green: 13, blue: 14 } puts DATA.readlines.sum { |line| game, rounds = line.match(/Game (\d+): (.+)/).captures rounds = rounds.split("; ").map { _1.scan(/(\d+) (\w+)/) } next 0 unless rounds.all? { |round| round.all? { |count, color| count.to_i <= CONSTRAINTS[color.to_sym] } } game.to_i } __END__ Data goes here