Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Solution to Google Treasure Hunt 2008 - Task 2

The program below can be used to solve a particular instance of the task presented at http://treasurehunt.appspot.com (Task 2, 2008). The input to countLines calls would probably have to change, otherwise it should work by executing as this:

./googlehunt Google-something.zip

And the source code is as follows (put into Public Domain by Lars Ivar Igesund):

module googlehunt;

import tango.io.vfs.ZipFolder;
import tango.text.Util;
import tango.io.Stdout;
import tango.text.stream.LineIterator;
import tango.text.convert.Integer;

void main(char[][] args)
{
    if (args.length < 2)
        return;

    auto archive = new ZipFolder(args[1]);
    auto info = archive.tree;

    ulong countLines(char[] ext, char[] pattern, int ln) {

        bool googleFilter(VfsInfo info) {
            if (info.path.containsPattern(pattern) ||
                info.name.containsPattern(pattern))
                if (info.name[$-ext.length..$] == ext)
                    return true;

            return false;
        }

        ulong sum;
        foreach (file; info.catalog(&googleFilter)) {
            foreach (idx, line; new LineIterator!(char) (file.input))
                if (idx == ln)
                    if (line.length > 0)
                        sum += toInt(line);
        }

        return sum;
    }

    ulong sum1, sum2;
    sum1 = countLines(".xml", "BCD", 4);
    sum2 = countLines(".js", "stu", 3);

    Stdout ("Sum 1 = ")(sum1)(", sum 2 = ")(sum2).newline;
    Stdout ("Result is: ")(sum1 * sum2).newline;
}