yhpg009.rb
def discount_sub(map, type, a)
if a > map[type]
a -= map[type]
map[type] = 0
a
else
map[type] -= a
0
end
end
def discount(map)
a = (map["An"]+map["Aw"]+map["Ap"]) * 2
a = discount_sub(map, "In", a)
discount_sub(map, "Iw", a)
end
def half(x)
(x+1) / 2
end
def fee(map, u)
u * map["An"] +
half(u) * (map["Aw"]+map["Cn"]+map["In"]) +
half(half(u)) * (map["Cw"]+map["Iw"])
end
def calculate(line)
map = Hash.new(0)
(unit, members) = line.split("0:")
members.split(",").each{|x| map[x] += 1}
discount(map)
fee(map, unit.to_i)*10
end
while line=gets
puts calculate(line.chomp!)
end