puzzle contents
Contents
raw puzzle

Original Problem

A digital river is a sequence of numbers where every number is followed by the same number plus the sum of its digits. In such a sequence 123 is followed by 129 (since 1 + 2 + 3 = 6), which again is followed by 141.

We call a digital river river K, if it starts with the value K.

For example, river 7 is the sequence beginning with {7, 14, 19, 29, 40, 44, 52, ... } and river 471 is the sequence beginning with {471, 483, 498, 519, ... }.

Digital rivers can meet. This happens when two digital rivers share the same values. River 32 meets river 47 at 47, while river 471 meets river 480 at 519.

Given two meeting digital rivers print out the meeting point.

Input
Line 1: The first river r1.
Line 2: The second river r2.
Output
Line 1 : The meeting point of the rivers given.
Constraints
0 < r1 ≤ 20000000
0 < r2 ≤ 20000000

Solution

After implementing a function to calculate the digit sum of a number or using Ruby with its built-in digits.sum method, the algorithm is already described by the problem statement. We add the digits sum to the initial variables as long as they don't meet anymore. To keep them leveled, we continue on r1 as long as r1 is smaller than r2 and continue on r2 if r1 surpasses r2:

r1 = gets.to_i
r2 = gets.to_i

while r1 != r2 do
  if r1 < r2
    r1+= r1.digits.sum
  else
    r2+= r2.digits.sum
  end  
end

puts r1