solve problem 12 with ruby