advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

count.sh (550B)


      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
#!/bin/sh

[ "$#" = 1 ] || {
    printf "usage: %s inputfile\n" "$0" >&2
    exit 1
}

max=0
cur=0
while read -r line; do
    case "$line" in '')
        [ "$cur" -gt "$max" ] && max="$cur"
        cur=0
    ;;
    [0-9]*)
        printf "%d\n" "$line" >/dev/null 2>&1 || {
            printf "%s is not a number\n" "$line" >&2
            exit 1
        }
        cur=$((cur + line))
    ;;
    *)
        printf "%s is not a number\n" "$line" >&2
        exit 1
    ;;
    esac
done < "$1"
[ "$cur" -gt "$max" ] && max="$cur"

printf "%d\n" "$max"