COLORS = %w[ red green blue ] puts DATA.readlines.sum { |line| _game, rounds = line.match(/Game (\d+): (.+)/).captures rounds = rounds.split("; ").map { _1.scan(/(\d+) (\w+)/) } COLORS.map { |color| rounds.filter_map { |round| round.find { _2 == color }&.first&.to_i }.max }.reduce(:*) # Alt: #COLORS.map { |color| rounds.flat_map { |round| round.filter_map { _1.to_i if _2 == color } }.max }.reduce(:*) } __END__ Data goes here