aboutsummaryrefslogtreecommitdiff
path: root/challenge-152/abigail/ruby/ch-1.rb
blob: 7e8132e448c62e789a2a72092acd633d24deb58f (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
28
29
30
#!/usr/bin/ruby

#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152
#
 
#
# Run as: ruby ch-1.rb < input-file
#

ARGF . each_line do |line|
    minsum = 0
    n      = 1
    m      = n
    min    = 0
    line . split . map do |num| 
        num = num . to_i
        if n == m or num < min then
            min = num
        end
        m -= 1
        if m == 0 then
            minsum += min
            n      += 1
            m       = n
            min     = 0
        end
    end
    puts (minsum)
end