solved problem 18 again in ruby