package yuku.gth2008;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;

public class Q2 {
    Vector<File> xfile = new Vector<File>();
    
    public static void main(String[] args) {
        new Q2().u();
    }

    private void u() {
        String root = "GoogleTreasureHunt08_14078660674866096782";
        
        jelajah(new File(root));
        
        int sum1 = 0, sum2 = 0;
        
        for (File file: xfile) {
            String nf = file.toString();
            if (nf.contains("foo") && nf.endsWith(".txt")) {
                sum1 += baca(file, 1);
            }
            if (nf.contains("foo") && nf.endsWith(".pdf")) {
                sum2 += baca(file, 4);
            }
        }
        
        long jaw = (long)sum1 * sum2;
        System.out.println(jaw);
    }

    private int baca(File file, int ke) {
        try {
            Scanner sc = new Scanner(file);
            int pos = 1;
            
            while (sc.hasNextLine()) {
                String b = sc.nextLine();
                
                if (pos == ke) {
                    sc.close();
                    System.out.println(file + ":" + ke + " = " + b);
                    return Integer.parseInt(b);
                }
                
                pos++;
            }
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return 0;
    }

    private void jelajah(File root) {
        File[] xc = root.listFiles();
        for (File c: xc) {
            if (c.isDirectory()) {
                jelajah(c);
            } else {
                xfile.add(c);
            }
        }
    }
}
