Wednesday, 21 May 2008

Google Treasure Hunt - How to solve the second problem

Warning: spoilers contained within!
How to Solve the Second question in the Google Treasure Hunt 2008
question: 2 sums from line 4 in *BCD*.xml, line 2 in *def*.js)
Unlike the previous question (the robot on grid), I was able to solve the second question without too much difficulty. After about 20 minutes spent looking up bash manuals online, I came up with the following script:

for file in $(find . -maxdepth 999 -path '*BCD*' -or -name '*BCD*' | grep ".xml")
tail +4 $file | head -1
echo "----"
for file in $(find . -maxdepth 999 -path '*def*' -or -name '*def*' | grep ".js")
tail +2 $file | head -1

This script basically uses the linux utility "find" coupled with "grep" to locate all files matching the pattern specified. Then, for each file, I piped the contents through "tail" and "head" to obtain the desired line, which is printed to the standard output.

The sharp-eyed among you might have noticed that this script doesn't actually calculate the sum of all the lines, or multiply these sums together. This is because I actually used the lazy-man's calculator, Google, to complete this part of the problem. Simply copy and paste the command line output from this script into Google, insert "+" symbols between each number, and multiply the two outputs using the same method.

I know this code is "spaghetti" code, but I'm too lazy to fix it up. Please post a comment if you're enough of a nerd to format this in intelligible code.

Now, to wait for the next one...... :)


Devendra said...

Wonderful.. thanks. I just copied your code and replaced my params and Hurrraaaaaaay!!! it worked.


David said...

Glad you found this useful. I was hoping somebody could benefit from this! I'm certainly looking forward to the next question, what about you?

Dev said...

Thanks.. Yeah i am also looking for next question.