import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;

public class ReadFile {
	public static void main(String[] args) throws Exception {
		String srcFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt1.in.txt";
		String destFile = "C:" + File.separator + "Documents and Settings"
				+ File.separator + "rohit" + File.separator + "My Documents"
				+ File.separator + "Downloads" + File.separator
				+ "C-small-attempt0.out";
		File file = new File(srcFile);
		List<String> readData = new ArrayList<String>();
		FileReader fileInputStream = new FileReader(file);
		BufferedReader bufferedReader = new BufferedReader(fileInputStream);
		StringBuilder stringBuilder = new StringBuilder();
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			readData.add(line);
		}
		stringBuilder.append(generateOutput(readData));
		File writeFile = new File(destFile);
		if (!writeFile.exists()) {
			writeFile.createNewFile();
		}
		FileWriter fileWriter = new FileWriter(writeFile);
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
		bufferedWriter.write(stringBuilder.toString());
		bufferedWriter.close();
		System.out.println("output" + stringBuilder);
	}

	/**
	 * @param number
	 * @return
	 */
	private static String generateOutput(List<String> number) {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < number.size(); i++) {
			if (i == 0) {
				continue;
			} else {
				String tempString = number.get(i);
				String[] temArr = tempString.split(" ");
				stringBuilder.append("Case #" + i + ": ");
				stringBuilder.append(getRecNumbers(temArr[0], temArr[1]));
				stringBuilder.append("\n");
			}
		}
		if (stringBuilder.length() > 0) {
			stringBuilder.deleteCharAt(stringBuilder.length() - 1);
		}
		return stringBuilder.toString();
	}

	// /* This method accepts method for google code jam */
	// private static String method(String value) {
	// int intValue = Integer.valueOf(value);
	// double givenExpr = 3 + Math.sqrt(5);
	// double num = Math.pow(givenExpr, intValue);
	// String valArr[] = (num + "").split("\\.");
	// int finalVal = Integer.valueOf(valArr[0]) % 1000;
	//
	// return String.format("%1$03d", finalVal);
	//
	// }
	//
	// /* This method accepts method for google code jam */
	// private static String method2(String value) {
	// StringTokenizer st = new StringTokenizer(value, " ");
	// String test[] = value.split(" ");
	// StringBuffer str = new StringBuffer();
	//
	// for (int i = 0; i < test.length; i++) {
	// // test[i]=test[test.length-i-1];
	// str.append(test[test.length - i - 1]);
	// str.append(" ");
	// }
	// str.deleteCharAt(str.length() - 1);
	// String strReversedLine = "";
	//
	// while (st.hasMoreTokens()) {
	// strReversedLine = st.nextToken() + " " + strReversedLine;
	// }
	//
	// return str.toString();
	// }

	private static int getRecNumbers(String no1, String no2) {

		int a = Integer.valueOf(no1);
		int b = Integer.valueOf(no2);
		Set<String> dupC = new HashSet<String>();
		for (int i = a; i <= b; i++) {
			int n = i;
			List<String> value = findPerm(n);
			for (String val : value) {
				if (Integer.valueOf(val) <= b && Integer.valueOf(val) >= a
						&& n < Integer.valueOf(val)) {
					dupC.add(n + "-" + val);
				}
			}
		}
		return dupC.size();
	}

	private static List<String> findPerm(int num) {
		String numString = String.valueOf(num);
		List<String> value = new ArrayList<String>();
		for (int i = 0; i < numString.length(); i++) {
			String temp = charIns(numString, i);
			if (!temp.equalsIgnoreCase(numString)) {
				value.add(charIns(numString, i));
			}
		}
		return value;

	}

	public static String charIns(String str, int j) {
		String begin = str.substring(0, j);
		String end = str.substring(j);
		return end + begin;
	}
}
package de.at.codejam.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import de.at.codejam.util.CodeJamConstants;
import de.at.codejam.util.StatusListener;
import de.at.codejam.util.TaskStatus;

public class CodeJamStatusPanel extends JPanel implements CodeJamConstants,
		StatusListener {

	private static final long serialVersionUID = -4645339049484500917L;

	private int currentLogLevelTreshold = StatusListener.LOGLEVEL_INFO;

	private JPanel statusIconPanel = new JPanel() {

		private static final long serialVersionUID = -1109894604603210990L;

		public Insets getInsets() {
			return new Insets(10, 10, 10, 10);
		}
	};

	private JTextArea consoleArea = new JTextArea();

	private JProgressBar progressBar = new JProgressBar();

	public CodeJamStatusPanel() {

		super(new BorderLayout());

		setPreferredSize(DEFAULT_STATUSPANEL_SIZE);

		statusIconPanel.setBackground(Color.WHITE);
		statusIconPanel.setBorder(BorderFactory.createRaisedBevelBorder());
		statusIconPanel.setPreferredSize(new Dimension(128, 128));
		statusIconPanel.setSize(128, 128);
		add(statusIconPanel, BorderLayout.WEST);

		consoleArea.setText("All console input goes here ..." + '\n');

		JPanel consolePanel = new JPanel();
		consolePanel.setLayout(new BorderLayout());
		consolePanel.add(new JScrollPane(consoleArea), BorderLayout.CENTER);

		JPanel loglevelPanel = new JPanel(new BorderLayout());

		loglevelPanel.setBorder(BorderFactory.createTitledBorder("LogLevel"));

		JRadioButton loglevelButtonTrace = new JRadioButton("Trace");
		loglevelButtonTrace.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				CodeJamStatusPanel.this.currentLogLevelTreshold = StatusListener.LOGLEVEL_TRACE;
			}
		});
		JRadioButton loglevelButtonInfo = new JRadioButton("Info");
		loglevelButtonInfo.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				CodeJamStatusPanel.this.currentLogLevelTreshold = StatusListener.LOGLEVEL_INFO;
			}
		});
		JRadioButton loglevelButtonWarning = new JRadioButton("Warning");
		loglevelButtonWarning.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				CodeJamStatusPanel.this.currentLogLevelTreshold = StatusListener.LOGLEVEL_WARN;
			}
		});
		JRadioButton loglevelButtonError = new JRadioButton("Error");
		loglevelButtonError.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				CodeJamStatusPanel.this.currentLogLevelTreshold = StatusListener.LOGLEVEL_ERROR;
			}
		});

		if (StatusListener.LOGLEVEL_TRACE == currentLogLevelTreshold) {
			loglevelButtonTrace.setSelected(true);
		} else if (StatusListener.LOGLEVEL_INFO == currentLogLevelTreshold) {
			loglevelButtonInfo.setSelected(true);
		} else if (StatusListener.LOGLEVEL_WARN == currentLogLevelTreshold) {
			loglevelButtonWarning.setSelected(true);
		} else if (StatusListener.LOGLEVEL_ERROR == currentLogLevelTreshold) {
			loglevelButtonError.setSelected(true);
		}

		ButtonGroup loglevelGroup = new ButtonGroup();
		loglevelGroup.add(loglevelButtonTrace);
		loglevelGroup.add(loglevelButtonInfo);
		loglevelGroup.add(loglevelButtonWarning);
		loglevelGroup.add(loglevelButtonError);

		JPanel levelGrid = new JPanel(new GridLayout(1, 4));
		levelGrid.add(loglevelButtonTrace);
		levelGrid.add(loglevelButtonInfo);
		levelGrid.add(loglevelButtonWarning);
		levelGrid.add(loglevelButtonError);
		loglevelPanel.add(levelGrid);
		consolePanel.add(loglevelPanel, BorderLayout.NORTH);

		JPanel progressPanel = new JPanel(new BorderLayout());
		progressPanel.setBorder(BorderFactory.createTitledBorder("Progress"));
		progressPanel.add(progressBar, BorderLayout.CENTER);
		consolePanel.add(progressPanel, BorderLayout.SOUTH);

		add(consolePanel, BorderLayout.CENTER);
	}

	@Override
	public Insets getInsets() {
		return new Insets(10, 10, 10, 10);
	}

	@Override
	public void updateStatus(TaskStatus taskStatus) {

		progressBar.setStringPainted(true);

		boolean indeterminate = (UNDEFINED == taskStatus.getNumberCases())
				|| (UNDEFINED == taskStatus.getNumberCurrentCase());

		if (!indeterminate) {

			progressBar.setMaximum(taskStatus.getNumberCases());
			progressBar.setMinimum(1);
			progressBar.setValue(taskStatus.getNumberCurrentCase());
		}
		progressBar.setIndeterminate(indeterminate);

		if (TaskStatus.STATUS_WAITING == taskStatus.getCurrentTaskStatus()) {
			progressBar.setString("Waiting");
			statusIconPanel.setBackground(COLOR_WAITING);
		} else if (TaskStatus.STATUS_RUNNING == taskStatus
				.getCurrentTaskStatus()) {
			progressBar.setString("Running");
			statusIconPanel.setBackground(COLOR_RUNNING);
		} else if (TaskStatus.STATUS_DONE == taskStatus.getCurrentTaskStatus()) {
			progressBar.setString("Done");
			statusIconPanel.setBackground(COLOR_DONE);
		} else if (TaskStatus.STATUS_ERROR == taskStatus.getCurrentTaskStatus()) {
			progressBar.setString("Sad Panda");
			statusIconPanel.setBackground(COLOR_ERROR);
		}
	}

	@Override
	public void log(int logLevel, String message) {

		if (logLevel > currentLogLevelTreshold) {
			return;
		}

		StringBuilder newConsoleText = new StringBuilder(consoleArea.getText());
		newConsoleText.append("\n");
		newConsoleText.append(message);

		consoleArea.setText(newConsoleText.toString());
	}
}
