Solved #2 in ruby.