Trains problem solved in Ruby