Tuesday, May 20, 2008

Treasure Hunt Solution

Official Google Blog: Google Treasure Hunt update

The second question in the series is a small exercise in Unix.

The template of the question is thus:

Unzip the archive, then process the resulting files to obtain a numeric result. You'll be taking the sum of lines from files matching a certain description, and multiplying those sums together to obtain a final result. Note that files have many different extensions, like '.pdf' and '.js', but all are plain text files containing a small number of lines of text.

Sum of line L1 for all files with path or name containing W1 and ending in .EXT1
Sum of line L2 for all files with path or name containing W2 and ending in .EXT2
Hint: If the requested line does not exist, do not increment the sum.

Multiply all the above sums together and enter the product below.
(Note: Answer must be an exact, decimal representation of the number.)


Here's a dirty little script to do the job:
function TH() {
local sum=0
for file in `unzip -l $ZIP_FILE|sed -nre "/.*$1.*$2/p"|tr -s " "|cut -f5 -d" "`
do
val=`sed -n $3p ../$file`
if [ ! -z $val ];
then sum=$(($val+$sum));
fi
done
let sum_${4}=$sum
}
ZIP_FILE=$1
#TH $W $EXT $L
TH $2 $3 $4 1
TH $5 $6 $7 2
echo $sum_1*$sum_2 |bc

No comments: