30 lines
652 B
Ruby
30 lines
652 B
Ruby
|
#prepare the input which is a string containing new lines
|
||
|
def parse(input)
|
||
|
data = []
|
||
|
input.each_line do |line|
|
||
|
winners, draw = line.split(":").last.split("|")
|
||
|
winners =winners.split(" ").map {|entry| entry.strip.to_i }
|
||
|
draw = draw.split(" ").map {|entry| entry.strip.to_i }
|
||
|
data << [winners,draw]
|
||
|
end
|
||
|
data
|
||
|
end
|
||
|
|
||
|
# result should a single string or integer
|
||
|
def calculate(data)
|
||
|
cards = []
|
||
|
data.length.times.each do |i|
|
||
|
cards << 1
|
||
|
end
|
||
|
|
||
|
data.each_with_index do |d,i|
|
||
|
winners, draw = d
|
||
|
matches = (winners & draw).length
|
||
|
matches.times do |j|
|
||
|
cards[i+j+1] += cards[i]
|
||
|
end
|
||
|
end
|
||
|
|
||
|
cards.reduce(&:+)
|
||
|
end
|