/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codejam;

/**
 *
 * @author eblanco
 */
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.JFileChooser;

public class RecycledNumbers {

    public static String DatosArchivo[] = new String[10000];

    public static int[] convertStringArraytoIntArray(String[] sarray) throws Exception {
        if (sarray != null) {
            int intarray[] = new int[sarray.length];
            for (int i = 0; i < sarray.length; i++) {
                intarray[i] = Integer.parseInt(sarray[i]);
            }
            return intarray;
        }
        return null;
    }

    public static boolean CargarArchivo(String arch) throws FileNotFoundException, IOException {
        FileInputStream arch1;
        DataInputStream arch2;
        String linea;
        int i = 0;

        if (arch == null) {
            //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            JFileChooser fc = new JFileChooser(System.getProperty("user.dir"));
            int rc = fc.showDialog(null, "Select a File");
            if (rc == JFileChooser.APPROVE_OPTION) {
                arch = fc.getSelectedFile().getAbsolutePath();
            } else {
                System.out.println("No hay nombre de archivo..Yo!");
                return false;
            }
        }
        arch1 = new FileInputStream(arch);
        arch2 = new DataInputStream(arch1);
        do {
            linea = arch2.readLine();
            DatosArchivo[i++] = linea;
            /* if (linea != null) {
            System.out.println(linea);
            }*/
        } while (linea != null);
        arch1.close();
        return true;
    }

    public static boolean GuardaArchivo(String arch, String[] Datos) throws FileNotFoundException, IOException {
        FileOutputStream out1;
        DataOutputStream out2;
        int i;
        out1 = new FileOutputStream(arch);
        out2 = new DataOutputStream(out1);

        for (i = 0; i < Datos.length; i++) {
            if (Datos[i] != null) {
                out2.writeBytes(Datos[i] + "\n");
            }
        }
        out2.close();
        return true;
    }

    public static void echo(Object msg) {
        System.out.println(msg);
    }

    public static void main(String[] args) throws IOException, Exception {
        String[] res = new String[10000], num = new String[2];
        int i, j, k, ilong;
        int ele = 1;
       ArrayList al = new ArrayList();
        String FName = "C-small-attempt0",  istr, irev, linea;
        if (CargarArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".in")) {
            for (j = 1; j <= Integer.parseInt(DatosArchivo[0]); j++) {
                linea = DatosArchivo[ele++];
                num = linea.split(" ");
                   al.clear();
                // echo("A: "+num[0]+" B: "+num[1]);
                for (i = Integer.parseInt(num[0]); i <= Integer.parseInt(num[1]); i++) {
                    istr = Integer.toString(i);
                    ilong = istr.length();
                 
                    for (k = 1; k < ilong; k++) {
                        if (ilong > k) {
                            irev = istr.substring(istr.length() - k, istr.length()) + istr.substring(0, istr.length() - k);
                            //echo("caso: " + j + ": isrt: " + istr + " irev: " + irev);
                            if ((Integer.parseInt(irev) > Integer.parseInt(num[0])) && (Integer.parseInt(irev) > Integer.parseInt(istr)) && (Integer.parseInt(irev) <= Integer.parseInt(num[1]))) {
                                al.add("(" + istr + "," + irev + ")");
                            }
                        }
                    }
                }
                HashSet hs = new HashSet();
                hs.addAll(al);
                al.clear();
                al.addAll(hs);
                res[j] = "Case #" + j + ": " + al.size();
                echo(res[j]);
                LeerArchivo.GuardaArchivo("C:\\eblanco\\Desa\\CodeJam\\Code Jam\\test\\" + FName + ".out", res);
            }
        }
    }
}

package google.solver;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

public class ProblemPackageCreator  implements ChallengeConstants{

	private final String packageName;
	private String directoryName;
	private String challengeDef;
	private String readerName;
	private String challengeName;
	private String packagePath;


	ProblemPackageCreator(String packageName){
		this.packageName = packageName;
		packagePath = "google.contest."+packageName;
		directoryName = BASE_DIR+DELIMITER+packageName;
		challengeDef = BASE_DIR+DELIMITER+packageName+DELIMITER+CHALLENGE_DEFINITION;
		readerName = packageName.substring(0,1).toUpperCase()+packageName.substring(1)+"Reader";
		challengeName = packageName.substring(0,1).toUpperCase()+packageName.substring(1)+"Challenge";
	}

	private void create() {
		try{
			createDirectory();
			createTestIn();
			createChallengeDefinition2();
			createReader();
			createChallenge();
		}
		catch(Exception e){
			throw new RuntimeException(e);
		}
	}

	private void createTestIn() {
		writeToPath(TEST_IN, "");
		
	}

	private void createChallenge() {
		String def ="package "+packagePath+";\n\n"+
				"import google.loader.Challenge;\n"+
				"import google.problems.AbstractChallenge;\n\n"+
				"public class "+challengeName+" extends AbstractChallenge implements Challenge {\n\n"+
				"public "+challengeName+"(int number) {\n"+
				"super(number);\n"+
				"setResult(\"\");\n"+
				"}\n"+
				"}\n";
		writeJava(challengeName, def);
	}

	private void createReader() {
		String def = "package "+packagePath+";\n\n"+
				"import google.loader.Challenge;\n"+
				"import google.problems.AbstractReader;\n\n"+
				"public class "+readerName+" extends AbstractReader {\n \n"+
				"@Override\n"+
				"protected Challenge createChallenge(int number) {\n"+
				"int theLine = getActualLine();\n"+
				"String[] lines = getLines();\n"+
				"int value = Integer.parseInt(getLines()[theLine]);\n"+
				challengeName+" chal = new "+challengeName+"(number);\n"+
				"setActualLine(theLine+1);\n"+
				"return chal;\n"+
				"}\n"+
				"\n"+
				"}\n";

		writeJava(readerName, def);
	}
	
	private void writeToPath(String fileName, String text){
		write(directoryName+DELIMITER+fileName, text);
	}

	private void writeJava(String baseName, String text) {
		writeToPath(baseName+".java", text);
	}

	private void createChallengeDefinition() {
		String def = "fileName="+TEST_IN+"\n"+
				"# fileName=A-small-practice.in\n"+
				"# fileName=A-large-practice.in\n"+
				"# fileName=B-small-practice.in\n"+
				"# fileName=B-large-practice.in\n"+
				"# fileName=C-small-practice.in\n"+
				"# fileName=C-large-practice.in\n"+
				"readerClassName="+packagePath+"."+readerName;
		write(challengeDef, def);
	}

	private void createChallengeDefinition2() {
		String def = "fileName="+TEST_IN+"\n"+
				"# fileName="+packageName+"-small-practice.in\n"+
				"# fileName="+packageName+"-large-practice.in\n"+
				"readerClassName="+packagePath+"."+readerName;
		write(challengeDef, def);
	}
	
	private void createDirectory() throws Exception{
		File file = new File(directoryName);
		if(! file.exists()){
			file.mkdir();
		}else{
			throw new RuntimeException("alreadyExisting");
		}
	}

	public static void main(String[] args) {
		String packageName =CHALLENGE_NAME;
		ProblemPackageCreator creator = new ProblemPackageCreator("A");
		creator.create();
		creator = new ProblemPackageCreator("B");
		creator.create();
		creator = new ProblemPackageCreator("C");
		creator.create();
	}

	public static void write(String aFileName, String text) {
		try{
			FileWriter fstream = new FileWriter(aFileName);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(text);
			out.close();
		}catch (Exception e){
			throw new RuntimeException(e);
		}
	}


}
