#!/bin/bash
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# Copyright (C) 2008 Vino Fernando Crescini
# Google Treasure Hunt 2008 Puzzle 2
# arg1 is the search pattern, arg2 is the line number
function find_n_count
{
find . | grep "$1" | while read i; do
awk "NR == $2 { print \$0; }" "$i"
done | awk 'BEGIN { tot = 0; } { tot += $0; } END { print tot; }'
}
function is_num
{
tmp=${1//[0-9]/}
if [ "$tmp" != "" ]; then
return 1
fi
return 0
}
if [ $# -ne 4 ]; then
echo "usage: $0 "
exit 1
fi
if ! is_num $3 && ! is_num $4; then
echo "third and fourth arguments must be positive integers"
exit 2
fi
sum1=`find_n_count "$1" $3`
sum2=`find_n_count "$2" $4`
echo "$sum1 * $sum2" | bc -q
exit $?