Solution for the trains problem implemented in Ruby.