scores = {} 5.times do str, coin = gets.split scores[str] = coin.to_i end reels = [] 3.times do reel = [] gets.to_i.times do reel << gets.chomp end reels << reel end def check(reel1, reel2, reel3) sets = [] (0..2).each do |i| sets << reel1[i] if (reel1[i] == reel2[i] && reel2[i] == reel3[i]) end sets << reel1[0] if (reel1[0] == reel2[1] && reel2[1] == reel3[2]) sets << reel1[2] if (reel1[2] == reel2[1] && reel2[1] == reel3[0]) sets end sum = 0 count = Hash.new(0) (0..reels[0].size - 1).each do |i| (0..reels[1].size - 1).each do |j| (0..reels[2].size - 1).each do |k| sum += 1 check(reels[0].rotate(i), reels[1].rotate(j), reels[2].rotate(k)).each do |s| count[s] += 1 end end end end score = 0 count.each do |key, val| score += val * scores[key] end p score / sum.to_f scores.each_key do |key| p count[key] end