35 lines
813 B
Ruby
35 lines
813 B
Ruby
|
#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|
|
||
|
draws = d.values.first
|
||
|
mins = {
|
||
|
red: 0,
|
||
|
blue: 0,
|
||
|
green: 0
|
||
|
}
|
||
|
draws.each do |set|
|
||
|
set.each do |draw|
|
||
|
num, color = draw.strip.split(" ")
|
||
|
mins[color.to_sym] = num.to_i if num.to_i > mins[color.to_sym]
|
||
|
end
|
||
|
end
|
||
|
result += mins[:red] * mins[:blue] * mins[:green]
|
||
|
end
|
||
|
result
|
||
|
end
|