aboutsummaryrefslogtreecommitdiff
path: root/challenge-119/abigail/ruby/ch-2.rb
blob: 55ac672fca6d415da0c3fdcaf19bc179084ca6c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/ruby

#
# See ../README.md
#
 
#
# Run as: ruby ch-2.rb < input-file
#

def next_num (prev_num)
    ("0" + prev_num) . match (/^(.*)([012])(3*)$/) do
        |match|
        return (match [1] + (1 + match [2] . to_i) . to_s +
                match [3] . gsub(/3/, "1")) . gsub(/11/, "12") . gsub(/^0/, "")
    end
end


ARGF . each_line do
    |num|
    number = "0"
    for i in 1 .. num . to_i do
        number = next_num (number)
    end
    puts (number)
end