aboutsummaryrefslogtreecommitdiff
path: root/challenge-152/abigail/bash/ch-1.sh
blob: 941876c840ec3edabbb496a233864577da600aee (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
31
32
#!/bin/sh

#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-152
#

#
# Run as: bash ch-1.sh < input-file
#

set -f

while read -a F
do   ((minsum = 0))
     ((n      = 1))
     ((m      = n))
     ((min    = 0))
     for num in ${F[@]}
     do  if   ((n == m || num < min))
         then ((min = num))
         fi

         if   ((-- m == 0))
         then ((minsum += min))
              ((m       = ++ n))
              ((min     = 0))
         fi
     done

     echo $minsum

done