Solved #1 in ruby.